#include <iostream>
-#include <thread>
+#include <threads.h>
#include "seqlock.h"
-class IntWrapper {
- private:
- int _val;
- public:
+seqlock *lock;
- 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());
+int *readD1;
+int *readD2;
+static void read_thrd(void *obj) {
+ readD1 = new int;
+ readD2 = new int;
+ lock->read(readD1, readD2);
+ cout << "Read: d1 = " << *readD1 << ", d2 = " << *readD2 << endl;
}
-
-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_thrd1(void *obj) {
+ int d1 = 1, d2 = 0;
+ lock->write(1, 0);
+ cout << "Write: d1 = " << d1 << ", d2 = " << d2 << 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;
+static void write_thrd2(void *obj) {
+ int d1 = 1, d2 = 2;
+ lock->write(1, 2);
+ cout << "Write: d1 = " << d1 << ", d2 = " << d2 << endl;
}
-int main(int argc, char *argv[]) {
- /*
+int user_main(int argc, char *argv[]) {
+ lock = new seqlock;
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);
+ thrd_create(&t1, &read_thrd, NULL);
+ thrd_create(&t2, &write_thrd1, NULL);
+ thrd_create(&t3, &write_thrd2, NULL);
+
+ thrd_join(t1);
+ thrd_join(t2);
+ thrd_join(t3);
+ return 0;
}