more data structures
[cdsspec-compiler.git] / benchmark / seqlock / seqlock.cc
diff --git a/benchmark/seqlock/seqlock.cc b/benchmark/seqlock/seqlock.cc
new file mode 100644 (file)
index 0000000..f25434d
--- /dev/null
@@ -0,0 +1,53 @@
+#include <iostream>
+#include <thread>
+
+#include "seqlock.h"
+
+class IntWrapper {
+       private:
+           int _val;
+               public:
+
+               IntWrapper(int val) : _val(val) {}
+
+               IntWrapper() : _val(0) {}
+
+               IntWrapper(IntWrapper& copy) : _val(copy._val) {}
+
+               int get() {
+                       return _val;
+               }
+};
+
+static void* read_int(void *int_wrapper) {
+       IntWrapper *ptr = (IntWrapper*) int_wrapper;
+       return (void*) new int(ptr->get());
+}
+
+
+static IntWrapper *shared_int = new IntWrapper(10);
+static seqlock<IntWrapper> my_lock(shared_int);
+
+static void read_thrd(void *obj) {
+       void *res = my_lock.read(read_int);
+       cout << "Thread read: " << *((int*) res) << endl;
+}
+
+static void write_thrd(void *obj) {
+       IntWrapper *new_int = new IntWrapper(1024);
+       my_lock.write(new_int);
+       cout << "Thread write: " << new_int->get() << endl;
+}
+
+int main(int argc, char *argv[]) {
+       /*
+       thrd_t t1, t2, t3;
+       thrd_create(&t1, (thrd_start_t) &read_thrd, NULL);
+       thrd_create(&t2, (thrd_start_t) &write_thrd, NULL);
+       thrd_create(&t3, (thrd_start_t) &read_thrd, NULL);
+       */
+       read_thrd(NULL);
+       write_thrd(NULL);
+       read_thrd(NULL);
+       
+}