Reputation: 23
I have a code like this
Create thread_1 and bind task 1 to that;
Create thread_2 and bind task 2 to that;
Create thread_3 and it monitors the keyboards and work as follow:
while(true){
get the next key;
if (the next key == 'p')
pause_the_code();
if (the next key == 'r')
resume_the_code();
if(the next key == 'q'
exit the while loop
}
// can I here block the Thread_1 and _Thread_2 with all of their children
//with wait() when I call pause_the_code(); or notify them when I call resume_the_code();
Please note that hread_1 and Thread_2 each one can dynamically create different bunch of threads.
Upvotes: 0
Views: 191
Reputation: 54325
No you cannot reach out from one thread and pause or block another thread. Not in any safe way.
What you do is create a way for the threads to block themselves, by checking for it.
You could use an atomic boolean variable named something like running
and when it was false run a small loop with a sleep in it.
You could use a condition variable and a mutex protecting a boolean named running
and when it was false wait for a condition notification.
You could have each worker thread lock on a mutex. If thread_3 holds the mutex instead, they cannot take it and must wait until it is released.
Upvotes: 2