My Project
Loading...
Searching...
No Matches
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
8namespace LibThread {
9
10template <typename T>
11class Channel {
12private:
16 std::queue<T> q;
17public:
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 }
39 T result;
41 return result;
42 }
43};
44
45}
46
47#endif // _SINGULAR_LIBTHREAD_CHANNEL
void wait()
Definition: thread.h:88
void signal()
Definition: thread.h:97
std::queue< T > q
Definition: channel.h:16
void receive(T &result)
Definition: channel.h:27
void send(T &value)
Definition: channel.h:20
ConditionVariable cond
Definition: channel.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