Hello,
I was playing with concurrency (and I'm relatively new to it). I made a class so that you can wait for a boolean value to become true, called the "WaitableBool":
Code:
class WaitableBool{
public:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Mutex mutex;
Semaphore sem;
int waitercount;
bool value;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WaitableBool(bool init):
value(init),
waitercount(0)
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void waitUntilTrue();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setValue(
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
};
Code:
void WaitableBool::waitUntilTrue(){
if(value == true)
return;
mutex.Lock();
waitercount++;
mutex.Unlock();
sem.Wait();
}
void WaitableBool::setValue(bool theval){
mutex.Lock();
value = theval;
if(theval == true){
// then all those waiting needs to be told that it's done.
int i;
for(i = 0; i < waitercount; i++){
sem.Post();
}
waitercount = 0;
}
mutex.Unlock();
}
Now, the question that I have is.. is there an existing concurrency thing that lets you do this? Did I just reinvent the wheel? The idea is, many threads could be waiting for a boolean value to turn to "true", and many threads could also change the value of this bool.
So yeah, what do you think?
Thanks in advance