Merge branch 'master' of /scratch/mine/libcds
[libcds.git] / test / stress / misc / barrier_driver.cpp
1 #include <atomic>
2 #include <cds/gc/dhp.h>
3 #include <cds/gc/hp.h>
4 #include <cds/sync/barrier.h>
5 #include <cds_test/stress_test.h>
6 #include <iostream>
7 #include <thread>
8
9 using namespace std;
10
11 namespace {
12
13 typedef cds_others::SpinBarrier Barrier;
14
15 class BarrierTest : public cds_test::stress_fixture {
16 protected:
17   static Barrier *barrier;
18   static int count;
19   static const int kThreads = 6;
20   static const int kPassCount = 10000;
21
22   static void SetUpTestCase() {}
23
24   static void TearDownTestCase() {
25     assert (count == kPassCount*kPassCount);
26   }
27
28   static void Thread() {
29     for (int i = 0; i < kPassCount; i++) {
30       for (int j = 0; j < kPassCount; j++) {
31         if (barrier->wait()) {
32           count++;
33         }
34       }
35     }
36   }
37 };
38
39 Barrier *BarrierTest::barrier;
40 int BarrierTest::count;
41 const int BarrierTest::kThreads;
42
43 TEST_F(BarrierTest, Wait) {
44   barrier = new Barrier(kThreads);
45   int num_threads = kThreads;
46   std::thread *threads = new std::thread[num_threads];
47   for (int i = 0; i < num_threads; i++) {
48     threads[i] = std::thread(Thread);
49   }
50
51   for (int i = 0; i < num_threads; i++) {
52     threads[i].join();
53   }
54 }
55
56 } // namespace