Boost condition variables
Nov 27, 2012 at 9:25pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
template <typename T>
class Event{
bool ready;
T var;
boost::condition_variable cv;
boost::mutex mutex;
public :
Event():ready(0){}
void set(const T &var){
boost::unique_lock<boost::mutex> lock(this ->mutex);
this ->var=var;
this ->ready=1;
#if NOTIFY_ONE
this ->cv.notify_one();
#else
this ->cv.notify_all();
#endif
}
const T &wait(){
boost::unique_lock<boost::mutex> lock(this ->mutex);
while (!this ->ready)
this ->cv.wait(lock);
this ->ready=0;
return this ->var;
}
};
void PooledThread::running_thread(void *p){
PooledThread *_this=(PooledThread *)p;
while (_this->start_call_event.wait()){
_this->function(_this->parameter);
_this->parameter=0;
_this->function=0;
_this->call_ended_event.set(1);
}
}
void PooledThread::operator ()(threaded_function_t f,void *p){
this ->function=f;
this ->parameter=p;
this ->start_call_event.set(1);
}
void PooledThread::join(){
this ->call_ended_event.wait();
}
If !NOTIFY_ONE, the program behaves nicely. Otherwise, I see the CPU usage increase linearly over time (running_thread gets a new job at frequent regular intervals).
Why could this be?
Topic archived. No new replies allowed.