Loading...
Searching...
No Matches
syncvar.h
Go to the documentation of this file.
#ifndef _SINGULAR_LIBTHREAD_SYNCVAR
#define _SINGULAR_LIBTHREAD_SYNCVAR
#include "singthreads.h"
#include "thread.h"
#include <queue>
namespace LibThread {
template <typename T>
class SyncVar {
private:
Lock lock;
ConditionVariable cond;
int waiting;
bool init;
T value;
public:
SyncVar() : lock(), cond(&lock), waiting(0), init(false) {
}
void write(T& value) {
lock.lock();
if (!init) {
this->value = value;
this->init = true;
}
if (waiting)
cond.signal();
lock.unlock();
}
void read(T& result) {
lock.lock();
waiting++;
while (!init)
cond.wait();
waiting--;
if (waiting)
cond.signal();
lock.unlock();
}
bool try_read(T& result) {
bool success;
lock.lock();
success = init;
if (success) {
result = value;
}
lock.unlock();
return success;
}
};
}
#endif // _SINGULAR_LIBTHREAD_SYNCVAR
false
return false
Definition cfModGcd.cc:85
ConditionVariable
Definition thread.h:73
LibThread::SyncVar::read
void read(T &result)
Definition syncvar.h:31
LibThread::SyncVar::waiting
int waiting
Definition syncvar.h:15
LibThread::SyncVar::try_read
bool try_read(T &result)
Definition syncvar.h:41
LibThread::SyncVar::SyncVar
SyncVar()
Definition syncvar.h:19
LibThread::SyncVar::write
void write(T &value)
Definition syncvar.h:21
LibThread::SyncVar::init
bool init
Definition syncvar.h:16
LibThread::SyncVar::value
T value
Definition syncvar.h:17
LibThread::SyncVar::lock
Lock lock
Definition syncvar.h:13
LibThread::SyncVar::cond
ConditionVariable cond
Definition syncvar.h:14
Lock
Definition thread.h:17
result
return result
Definition facAbsBiFact.cc:76
T
STATIC_VAR jList * T
Definition janet.cc:30
LibThread
Definition bytebuf.h:8
singthreads.h
thread.h