My Project
Loading...
Searching...
No Matches
Singular
dyn_modules
systhreads
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
8
namespace
LibThread
{
9
10
template
<
typename
T>
11
class
SyncVar
{
12
private
:
13
Lock
lock
;
14
ConditionVariable
cond
;
15
int
waiting
;
16
bool
init
;
17
T
value
;
18
public
:
19
SyncVar
() :
lock
(),
cond
(&
lock
),
waiting
(0),
init
(
false
) {
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
false
return false
Definition:
cfModGcd.cc:84
ConditionVariable
Definition:
thread.h:73
ConditionVariable::wait
void wait()
Definition:
thread.h:88
ConditionVariable::signal
void signal()
Definition:
thread.h:97
LibThread::SyncVar
Definition:
syncvar.h:11
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
Lock::lock
void lock()
Definition:
thread.h:46
Lock::unlock
void unlock()
Definition:
thread.h:57
result
return result
Definition:
facAbsBiFact.cc:75
T
STATIC_VAR jList * T
Definition:
janet.cc:30
LibThread
Definition:
bytebuf.h:8
singthreads.h
thread.h
Generated on Mon Feb 27 2023 10:53:50 for My Project by
doxygen 1.9.5
for
Singular