A straigtforward way would be like this:
mutex g_mutex;
condition_variable g_cond;
bool flag = false;
void function1(){ // this function is called by other parts of the program
//some code
lock_guard<mutex> lock(g_mutex);
flag = true;
g_cond.notify_one();
}
void funtion2(){ //this function is also called by other parts of the program
//some other code
lock_guard<mutex> lock(g_mutex);
flag = true;
g_cond.notify_one();
}
void function3(){ //this function runs continuously, similar to a while loop with a 1ms sleep in it
{
unique_lock<mutex> lock(g_mutex);
g_cond.wait(lock, []{return flag;}); // wait here until func1 or func2 have been called
flag = false;
}
fireEvent();//fires an event to run some other code
}
int main() {
// your code goes here
return 0;
}
But this will block your function3
until one of the other twos are called. So it is a change of the behaviour and it adds additional lock contention.