Loading...
Searching...
No Matches
channel.h
Go to the documentation of this file.
#ifndef _SINGULAR_LIBTHREAD_CHANNEL
#define _SINGULAR_LIBTHREAD_CHANNEL
#include "singthreads.h"
#include "thread.h"
#include <queue>
namespace LibThread {
template <typename T>
class Channel {
private:
Lock lock;
ConditionVariable cond;
int waiting;
std::queue<T> q;
public:
Channel() : lock(), cond(&lock), waiting(0), q() {
}
void send(T& value) {
lock.lock();
q.push(value);
if (waiting)
cond.signal();
lock.unlock();
}
void receive(T& result) {
lock.lock();
waiting++;
while (q.empty())
cond.wait();
result = q.pop();
waiting--;
if (waiting)
cond.signal();
lock.unlock();
}
T receive() {
T result;
receive(&result);
return result;
}
};
}
#endif // _SINGULAR_LIBTHREAD_CHANNEL
ConditionVariable
Definition thread.h:73
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
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