Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 线程请解释输出_C++_Multithreading - Fatal编程技术网

C++ 线程请解释输出

C++ 线程请解释输出,c++,multithreading,C++,Multithreading,有一个代码示例: class MY_Timer { // ... void start(const UInt timeOut, const UInt events, CALLBACK_TARGET reciever) { cout << __FUNCTION__ << " " << timerName << endl; if (active) return;

有一个代码示例:

  class MY_Timer
    {
    // ...
    void start(const UInt timeOut, const UInt events, CALLBACK_TARGET reciever)
       {
          cout << __FUNCTION__ << " " << timerName << endl;

          if (active) return;
          if ((0u == timeOut) || (0u == events)) return;
          if (nullptr == reciever) return;

          interval = timeOut;
          eventsAmount = events;
          active = true;
          cb_target = reciever;

          thread clockieThread(&MY_Timer::clockie, this); // комментарий
          clockieThread.join();
       };

    private:
       void clockie()
       {
          while (eventsAmount--)
          {
             Sleep(interval);
             cb_target(timerName, eventsAmount);
          }

          active = false;
       }
    // ...
    };

    void target(const char * timerName, const UInt data)
    {
       cout << timerName << " DATA: " << data << endl;
    }

    int main()
    {
       MY_Timer * tOne = new MY_Timer("ALPHA");
       MY_Timer * tTwo = new MY_Timer("OMEGA");

       tOne->start(200, 10, &target);
       tTwo->start(300, 20, &target);
    }
您能解释一下为什么这个代码行为就像只有一个执行流一样吗。我认为输出将与来自两个线程的消息混合在一起,比如我将这样做:

void foo(const char * name, int interval) 
{
   int step = 10;
   while (step--)
   {
      Sleep(interval);
      cout << name << " step: " << step << endl;
   }
}

int main()
{
   thread t1(foo, "ALPHA", 200);
   thread t2(foo, "OMEGA", 300);
   t1.join();
   t2.join();

   return 0;
}
谢谢大家!

正如评论员所说,“join()”会阻塞主线程,直到新生成的线程完成为止,因此当您调用“start”函数时,它会创建线程,然后等待它完成

      thread clockieThread(&MY_Timer::clockie, this); // комментарий
      clockieThread.join();
您可以使用“std::thread::detach”让线程自行完成,但您将无法跟踪执行此操作的线程。

这是罪魁祸首:

      thread clockieThread(&MY_Timer::clockie, this); // комментарий
      clockieThread.join();
如果您考虑一下它的作用,并扩展代码,结果会有点像这样:

int main()
{
   MY_Timer * tOne = new MY_Timer("ALPHA");
   MY_Timer * tTwo = new MY_Timer("OMEGA");

   tOne->start(200, 10, &target);
   // clockieThread1 created
   // clockieThread1 joined (blocks until complete)
   tTwo->start(300, 20, &target);
   // clockieThread2 created
   // clockieThread2 joined (blocks until complete)

   return 0;
}
创建线程后,您将立即连接它,因此它会阻止所有内容,直到线程完成

您可能希望线程成为类的成员,并且您可以启动/加入它

class MY_Timer
{
  thread clockieThread;
  ...
  void start(const UInt timeOut, const UInt events, CALLBACK_TARGET reciever)
     ...
     clockieThread = thread(&MY_Timer::clockie, this);
     // Remove the clockieThread.join() here
  }

  void join() {
    clockieThread.join();
  }
}
然后,通过该更改,您可以执行以下操作:

int main()
{
   MY_Timer * tOne = new MY_Timer("ALPHA");
   MY_Timer * tTwo = new MY_Timer("OMEGA");

   tOne->start(200, 10, &target);
   tTwo->start(300, 20, &target);
   tOne->join();
   tTwo->join();

   return 0;
}
但是,如果您想完全消除
tOne->join()
调用,可以在类的析构函数中进行连接:

class MY_Timer
{
  ...
  ~MY_Timer() {
    clockieThread.join();
  }
}

您启动一个线程,然后立即加入,我不确定您期望的是什么。调用o'death:'clockieThread.join();'
int main()
{
   MY_Timer * tOne = new MY_Timer("ALPHA");
   MY_Timer * tTwo = new MY_Timer("OMEGA");

   tOne->start(200, 10, &target);
   tTwo->start(300, 20, &target);
   tOne->join();
   tTwo->join();

   return 0;
}
class MY_Timer
{
  ...
  ~MY_Timer() {
    clockieThread.join();
  }
}