I refer to the accepted answer here: Using std::conditional_variable to wait on a condition and in particular the code (copied):
struct gate {
bool gate_open = false;
mutable std::condition_variable cv;
mutable std::mutex m;
void open_gate() {
std::unique_lock<std::mutex> lock(m);
gate_open=true;
cv.notify_all();
}
void wait_at_gate() const {
std::unique_lock<std::mutex> lock(m);
cv.wait( lock, [this]{ return gate_open; } );
}
};
I don't understand how this works as an event class. How exactly does the code inside the mutex in open_gate
execute if something is already waiting via the wait_at_gate
functions. I'm guessing it has something to do with the std::condition_variable
.