Hi, I've got a small question:
What the code does is pretty simple, it waits for the WaitableBool "hasIncoming" to be true, then it locks incoming_lock..Code:Telepathic::Message TelepatiaChannel::getMessage(){ hasIncoming.waitUntilTrue(); // point 1 incoming_lock.Lock(); // point 2 assert(incoming.size() > 0); // point 3 Telepathic::Message msg = incoming.front(); incoming.pop_front(); incoming_lock.Unlock(); return msg; }
The WaitableBool class is from another thread I started months ago : http://cboard.cprogramming.com/showthread.php?t=95609
Now the question is, suppose I have two or more threads calling this function, all waiting at point 1. As soon as hasIncoming switches to true, one of the threads will move to point two. Is there a chance that, a thread (say, thread A) will take control inside point 1, and then lose control before it reaches point 2?
Because this could cause a situation where a thread thinks that there's available data, and comes and finds that there's no data, therefore triggering point 3..
I'm thinking that I have to actually change the code above, to make the thread wait again if it finds that there's no data.
So yeah, anybody know if my suspicion is right?
Thanks in advance