I have a situation where one thread produces events or triggers (e.g. a button press), and another thread wants to wait until that event occurs. However, when it waits, it doesn't care about previous events. I can almost do it like this:
class Trigger
{
public:
// Unblock all threads that are *currently* blocked in wait().
void notify()
{
mConditionVariable.notify_all();
}
// Wait until notify() is called.
void wait()
{
std::unique_lock<std::mutex> lock(mMutex);
mConditionVariable.wait(lock);
}
private:
std::mutex mMutex;
std::condition_variable mConditionVariable;
};
However, apparently mConditionVariable.wait() can spuriously unblock, so I can't rely on it. Is there any way to make this 100% reliable? Also is there a standard name for this construction?
Aucun commentaire:
Enregistrer un commentaire