more data structures
[cdsspec-compiler.git] / benchmark / seqlock / seqlock.cc
1 #include <iostream>
2 #include <thread>
3
4 #include "seqlock.h"
5
6 class IntWrapper {
7         private:
8             int _val;
9                 public:
10
11                 IntWrapper(int val) : _val(val) {}
12
13                 IntWrapper() : _val(0) {}
14
15                 IntWrapper(IntWrapper& copy) : _val(copy._val) {}
16
17                 int get() {
18                         return _val;
19                 }
20 };
21
22 static void* read_int(void *int_wrapper) {
23         IntWrapper *ptr = (IntWrapper*) int_wrapper;
24         return (void*) new int(ptr->get());
25 }
26
27
28 static IntWrapper *shared_int = new IntWrapper(10);
29 static seqlock<IntWrapper> my_lock(shared_int);
30
31 static void read_thrd(void *obj) {
32         void *res = my_lock.read(read_int);
33         cout << "Thread read: " << *((int*) res) << endl;
34 }
35
36 static void write_thrd(void *obj) {
37         IntWrapper *new_int = new IntWrapper(1024);
38         my_lock.write(new_int);
39         cout << "Thread write: " << new_int->get() << endl;
40 }
41
42 int main(int argc, char *argv[]) {
43         /*
44         thrd_t t1, t2, t3;
45         thrd_create(&t1, (thrd_start_t) &read_thrd, NULL);
46         thrd_create(&t2, (thrd_start_t) &write_thrd, NULL);
47         thrd_create(&t3, (thrd_start_t) &read_thrd, NULL);
48         */
49         read_thrd(NULL);
50         write_thrd(NULL);
51         read_thrd(NULL);
52         
53 }