#include #include #include #include #include using namespace std; mutex mutex_a; void function_that_does_stuff(){ for(;;){ sleep(1); mutex_a.lock(); // We want all this to happen together printf("beep "); printf("beep "); printf("beep "); printf("beep "); usleep(1); printf("beep\n"); mutex_a.unlock(); } } void function_that_clicks(){ for(;;){ sleep(1); mutex_a.lock(); printf("click "); printf("click "); printf("click "); printf("click "); usleep(1); printf("click\n"); mutex_a.unlock(); } } void unlock(int signal){ if(mutex_a.try_lock()) printf("Mutex was not locked! \n"); else printf("Mutex was locked, unlocking it now\n"); mutex_a.unlock(); } int main(){ struct sigaction handler; handler.sa_handler = unlock; sigaction(2, &handler, 0); thread t_a(function_that_does_stuff); thread t_b(function_that_clicks); for(;;) sleep(100); return 0; }