Refactors queue push pop
[libcds.git] / benchmark-drivers / treiber_stack_driver.cpp
1 #include <cds/init.h>
2 #include <cds/gc/dhp.h>
3 #include <cds/intrusive/treiber_stack.h>
4 #include <iostream>
5 #include <string>
6
7 using namespace std;
8
9 namespace ci = cds::intrusive;
10 typedef cds::gc::HP gc;
11
12 struct Foo : public ci::treiber_stack::node<gc> {
13   Foo(int x) : x(x) {}
14   int x;
15 };
16
17 struct MyTraits : public cds::intrusive::treiber_stack::traits {
18   typedef cds::intrusive::treiber_stack::stat<> stat;
19 };
20
21 // Equivalent make_traits example:
22 typedef cds::intrusive::TreiberStack<cds::gc::HP, Foo> MyStack;
23
24 int main() {
25   cds::Initialize();
26
27         {
28                 // Initialize Hazard Pointer singleton
29         cds::gc::HP hpGC(128, 8, 128);
30                 // If main thread uses lock-free containers
31                 // the main thread should be attached to libcds infrastructure
32                 cds::threading::Manager::attachThread();
33
34     MyStack s(1024);
35     Foo* f = new Foo(1);
36     Foo* res = nullptr;
37     s.push(*f);
38     res = s.pop();
39     if (res) {
40       cout << "Pop " << res->x << "\n";
41     } else {
42       cout << "Pop none\n";
43     }
44   }
45
46   cds::Terminate();
47   return 0;
48 }