C++ 我可以在C+;中阻止线程及其子线程吗+;

C++ 我可以在C+;中阻止线程及其子线程吗+;,c++,multithreading,concurrency,c++14,boost-asio,C++,Multithreading,Concurrency,C++14,Boost Asio,我有这样的代码 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_t

我有这样的代码

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();

请注意,Thread_1和Thread_2每个线程都可以动态创建不同的线程组。

不,您不能从一个线程伸出手来暂停或阻止另一个线程。不安全

您要做的是创建一种方法,通过检查线程来阻止它们自己

  • 您可以使用一个名为
    running
    的原子布尔变量,当它为false时,运行一个包含睡眠的小循环

  • 您可以使用一个条件变量和一个互斥锁来保护一个名为
    的布尔值,并在该值为false时等待条件通知

  • 您可以将每个工作线程锁定在互斥锁上。如果线程_3持有互斥锁,则它们不能接受互斥锁,必须等待它被释放


不,您不能从一个线程伸出手来暂停或阻止另一个线程。不安全

您要做的是创建一种方法,通过检查线程来阻止它们自己

  • 您可以使用一个名为
    running
    的原子布尔变量,当它为false时,运行一个包含睡眠的小循环

  • 您可以使用一个条件变量和一个互斥锁来保护一个名为
    的布尔值,并在该值为false时等待条件通知

  • 您可以将每个工作线程锁定在互斥锁上。如果线程_3持有互斥锁,则它们不能接受互斥锁,必须等待它被释放