My Project
Loading...
Searching...
No Matches
syncvar.h
Go to the documentation of this file.
1#ifndef _SINGULAR_LIBTHREAD_SYNCVAR
2#define _SINGULAR_LIBTHREAD_SYNCVAR
3
4#include "singthreads.h"
5#include "thread.h"
6#include <queue>
7
8namespace LibThread {
9
10template <typename T>
11class SyncVar {
12private:
16 bool init;
18public:
20 }
21 void write(T& value) {
22 lock.lock();
23 if (!init) {
24 this->value = value;
25 this->init = true;
26 }
27 if (waiting)
28 cond.signal();
29 lock.unlock();
30 }
31 void read(T& result) {
32 lock.lock();
33 waiting++;
34 while (!init)
35 cond.wait();
36 waiting--;
37 if (waiting)
38 cond.signal();
39 lock.unlock();
40 }
41 bool try_read(T& result) {
42 bool success;
43 lock.lock();
44 success = init;
45 if (success) {
46 result = value;
47 }
48 lock.unlock();
49 return success;
50 }
51};
52
53}
54
55#endif // _SINGULAR_LIBTHREAD_SYNCVAR
return false
Definition: cfModGcd.cc:84
void wait()
Definition: thread.h:88
void signal()
Definition: thread.h:97
void read(T &result)
Definition: syncvar.h:31
bool try_read(T &result)
Definition: syncvar.h:41
void write(T &value)
Definition: syncvar.h:21
ConditionVariable cond
Definition: syncvar.h:14
Definition: thread.h:17
void lock()
Definition: thread.h:46
void unlock()
Definition: thread.h:57
return result
Definition: facAbsBiFact.cc:75
STATIC_VAR jList * T
Definition: janet.cc:30