繁体   English   中英

线程和睡眠

[英]thread and Sleep

我试图了解多线程是如何工作的。

我有这个代码:

#include <iostream>
#include <thread>
#include <chrono>

void function1() {

   std::cout << "Hi I'm the function 1" << std::endl;
   std::this_thread::sleep_for(std::chrono::seconds(1));
   std::cout << "Hi I'm the function 1 after sleeping" << std::endl;

}

void function2() {

  std::cout << "Hi I'm the function 2" << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(5));
  std::cout << "Hi I'm the function 2 after sleeping" << std::endl;

}

int main()
{

  while(true) {

     std::thread t1(function1);
     std::thread t2(function2);

     t1.join();
     t2.join();

  }

  system("pause");
  return 0;

}

问题是当我运行它时,它停止等待std::this_thread::sleep_for(std::chrono::seconds(5)); 并且不显示下一个Hi I'm the function 1 from std::thread t1(function1); 在下一个循环中,直到睡眠线程结束。

1)你知道为什么吗?

2)我希望 main 继续循环,不要等待 t2 完成(函数 2 的 sleep_for() 设置为 5 秒)

这就是您的代码所做的:

  • 开始线程 1
    • 输出消息
    • 等待 1 秒
    • 输出另一个消息
  • 开始线程 2
    • 输出消息
    • 等待 5 秒
    • 输出另一个消息
  • 等待两个线程完成
    • (这大约需要 5 秒)
  • 无限重复

你已经声明这不是你想要做的。

我认为,相反,您打算每个线程内部进行“重复”,以便它们继续独立且无限期地滴答作响,如下所示:

#include <iostream>
#include <thread>
#include <chrono>

void function1() {
   while (true) {
      std::cout << "Hi I'm the function 1" << std::endl;
      std::this_thread::sleep_for(std::chrono::seconds(1));
      std::cout << "Hi I'm the function 1 after sleeping" << std::endl;
   }
}

void function2() {
  while (true) {
     std::cout << "Hi I'm the function 2" << std::endl;
     std::this_thread::sleep_for(std::chrono::seconds(5));
     std::cout << "Hi I'm the function 2 after sleeping" << std::endl;
   }
}

int main()
{
   std::thread t1(function1);
   std::thread t2(function2);

   t1.join();
   t2.join();
}

现在您的代码执行以下操作:

  • 开始线程 1
    • 输出消息
    • 等待 1 秒
    • 输出另一个消息
    • 无限重复
  • 开始线程 2
    • 输出消息
    • 等待 5 秒
    • 输出另一个消息
    • 无限重复
  • 等待两个线程完成
    • (虽然两者都不会!)

现在每个线程独立旋转,两者都不会“阻塞”另一个。

1)这是我的输出,似乎是我所期望的:

Hi I'm the function 1
Hi I'm the function 2
Hi I'm the function 1 after sleeping
Hi I'm the function 2 after sleeping
Hi I'm the function 1
Hi I'm the function 2
Hi I'm the function 1 after sleeping
Hi I'm the function 2 after sleeping
Hi I'm the function 2
Hi I'm the function 1
Hi I'm the function 1 after sleeping
Hi I'm the function 2 after sleeping
Hi I'm the function 2
Hi I'm the function 1
Hi I'm the function 1 after sleeping
Hi I'm the function 2 after sleeping
Hi I'm the function 2
Hi I'm the function 1

2)你说的最佳性能是什么意思??? sleep_for()无处不在,而Sleep是特定于 Windows 的......

我建议尽可能使用 std 库,放置睡眠的位置取决于您的上下文......

当您加入一个线程时,它将完成其执行并退出。 因此,当你加入你的线程 t1.join(); 和 t2.join();,第二个语句只有在第一个 join 语句完成时才会执行。 因此,在您的情况下,要连续折叠线程并并行执行,您必须分离线程,如下所示:-

int i = 0;
while(true) {

 std::thread t1(function1);
 std::thread t2(function2);

 t1.detach();
 t2.detach();

//also break your infinite loop here
if( ++i < 4)
   break;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM