barrier: modify to allow more than one reader
authorBrian Norris <computersforpeace@gmail.com>
Fri, 2 Nov 2012 04:57:22 +0000 (21:57 -0700)
committerBrian Norris <computersforpeace@gmail.com>
Fri, 2 Nov 2012 04:57:22 +0000 (21:57 -0700)
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

index 093da10f4106e8e7baef84d68c4756c1a6221656..5c99f650f1b0f625b515da1d51a7968a31d4c65d 100644 (file)
@@ -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;
 }