From a5efb65980dd2d82ed59a3b66d34a524e373a64d Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Thu, 1 Nov 2012 21:57:22 -0700 Subject: [PATCH] barrier: modify to allow more than one reader Just change the NUMREADERS macro to add more reader-threads. Having 3 or more threads is a more interesting example, since there's no contention if you just have one thread spin, waiting for the other. --- barrier/barrier.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/barrier/barrier.cc b/barrier/barrier.cc index 093da10..5c99f65 100644 --- a/barrier/barrier.cc +++ b/barrier/barrier.cc @@ -20,16 +20,21 @@ void threadB(void *arg) printf("var = %d\n", load_32(&var)); } +#define NUMREADERS 1 int user_main(int argc, char **argv) { - thrd_t t2, t3; + thrd_t A, B[NUMREADERS]; + int i; - barr = new spinning_barrier(2); + barr = new spinning_barrier(NUMREADERS + 1); - thrd_create(&t2, &threadA, NULL); - thrd_create(&t3, &threadB, NULL); - thrd_join(t2); - thrd_join(t3); + thrd_create(&A, &threadA, NULL); + for (i = 0; i < NUMREADERS; i++) + thrd_create(&B[i], &threadB, NULL); + + for (i = 0; i < NUMREADERS; i++) + thrd_join(B[i]); + thrd_join(A); return 0; } -- 2.34.1