Rewrites barrier driver
[libcds.git] / benchmark-drivers / vyukovqueue_driver.cpp
1 #include <cds/gc/dhp.h>
2 #include <cds/gc/hp.h>
3 #include <cds/init.h>
4 #include <cds/intrusive/vyukov_mpmc_cycle_queue.h>
5 #include <iostream>
6 #include <string>
7
8 using namespace std;
9
10 namespace ci = cds::intrusive;
11 typedef cds::gc::HP hp_gc;
12
13 struct Foo {
14   Foo(int x) : x(x) {}
15   int x;
16 };
17
18 typedef cds::intrusive::VyukovMPMCCycleQueue<
19     Foo, typename cds::intrusive::vyukov_queue::make_traits<cds::opt::buffer<
20              cds::opt::v::uninitialized_static_buffer<Foo, 1024>>>::type>
21     static_queue;
22
23 // Queue of Foo pointers, capacity is 1024, dynamically allocated buffer:
24 struct queue_traits : public cds::intrusive::vyukov_queue::traits {
25   typedef cds::opt::v::uninitialized_dynamic_buffer<Foo> buffer;
26 };
27 typedef cds::intrusive::VyukovMPMCCycleQueue<Foo, queue_traits> dynamic_queue;
28
29 int main() {
30   cds::Initialize();
31
32   {
33     // Initialize Hazard Pointer singleton
34     cds::gc::HP hpGC(128, 8, 128);
35     // If main thread uses lock-free containers
36     // the main thread should be attached to libcds infrastructure
37     cds::threading::Manager::attachThread();
38
39     static_queue q;
40     Foo *f = new Foo(1);
41     Foo *res = nullptr;
42     q.enqueue(*f);
43     res = q.dequeue();
44     if (res) {
45       cout << "Dequeued " << res->x << "\n";
46     } else {
47       cout << "Dequeued none\n";
48     }
49   }
50
51   cds::Terminate();
52   return 0;
53 }