My Project
Loading...
Searching...
No Matches
Singular
dyn_modules
systhreads
channel.h
Go to the documentation of this file.
1
#ifndef _SINGULAR_LIBTHREAD_CHANNEL
2
#define _SINGULAR_LIBTHREAD_CHANNEL
3
4
#include "
singthreads.h
"
5
#include "
thread.h
"
6
#include <queue>
7
8
namespace
LibThread
{
9
10
template
<
typename
T>
11
class
Channel
{
12
private
:
13
Lock
lock
;
14
ConditionVariable
cond
;
15
int
waiting
;
16
std::queue<T>
q
;
17
public
:
18
Channel
() :
lock
(),
cond
(&
lock
),
waiting
(0),
q
() {
19
}
20
void
send
(
T
& value) {
21
lock
.
lock
();
22
q
.push(value);
23
if
(
waiting
)
24
cond
.
signal
();
25
lock
.
unlock
();
26
}
27
void
receive
(
T
&
result
) {
28
lock
.
lock
();
29
waiting
++;
30
while
(
q
.empty())
31
cond
.
wait
();
32
result
=
q
.pop();
33
waiting
--;
34
if
(
waiting
)
35
cond
.
signal
();
36
lock
.
unlock
();
37
}
38
T
receive
() {
39
T
result
;
40
receive
(&
result
);
41
return
result
;
42
}
43
};
44
45
}
46
47
#endif
// _SINGULAR_LIBTHREAD_CHANNEL
ConditionVariable
Definition:
thread.h:73
ConditionVariable::wait
void wait()
Definition:
thread.h:88
ConditionVariable::signal
void signal()
Definition:
thread.h:97
LibThread::Channel
Definition:
channel.h:11
LibThread::Channel::q
std::queue< T > q
Definition:
channel.h:16
LibThread::Channel::receive
T receive()
Definition:
channel.h:38
LibThread::Channel::lock
Lock lock
Definition:
channel.h:13
LibThread::Channel::receive
void receive(T &result)
Definition:
channel.h:27
LibThread::Channel::Channel
Channel()
Definition:
channel.h:18
LibThread::Channel::waiting
int waiting
Definition:
channel.h:15
LibThread::Channel::send
void send(T &value)
Definition:
channel.h:20
LibThread::Channel::cond
ConditionVariable cond
Definition:
channel.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