C++ 线程和睡眠

C++ 线程和睡眠,c++,multithreading,performance,sleep,thread-sleep,C++,Multithreading,Performance,Sleep,Thread 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::c

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

我有以下代码:

#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;

}
#包括
#包括
#包括
无效函数1(){
std::cout1)这是我的输出,似乎是我所期望的:

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\u for()
适用于任何地方,而
sleep
是特定于windows的


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

这就是您的代码所做的:

  • 开始线程1
    • 输出消息
    • 等待1秒钟
    • 输出另一条消息
  • 开始线程2
    • 输出消息
    • 等待5秒钟
    • 输出另一条消息
  • 等待两个线程完成
    • (这大约需要5秒钟)
  • 无限期地重复
你说过这不是你想做的

我认为,相反,您打算让每个线程内部都有“repeat”,以便它们继续独立地、无限期地滴答作响,如下所示:

#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(){
while(true){

std::cout当您加入一个线程时,它将完成其执行并退出。因此,当您加入线程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;
}
inti=0;
while(true){
std::线程t1(函数1);
标准:螺纹t2(功能2);
t1.分离();
t2.分离();
//也在这里打破你的无限循环
如果(++i<4)
打破
}

“你知道为什么吗?”因为是你编程的。你启动两个线程,然后等待两个线程都完成。然后再次启动两个线程。如果你想让其中一个线程持续循环而不是等待另一个线程,你就需要编程。我怀疑你真的想在线程中设置循环,但当你没有告诉别人时,很难知道你想做什么是的,我想休眠线程,在其他程序中我想使用它,例如在游戏中,我需要在每次按键之间设置一个延迟,但同时我需要知道是否按下了GetAsyncKeyState()。不,我不想这样,因为如果function2设置为5秒,我希望main继续循环,不要等到t2完成。这会在你的头(和计算机)上创建太多线程将爆炸。这是真的。他必须控制他的无限循环。更正了我的答案。如果你在第一次迭代后停止它,它就不是无限循环。@LightnessRacesinOrbit,谢谢你的建议。我已经通过答案更正了。:-@LightnessRacesinOrbit,他想如何处理他的代码取决于他。尊重你的感觉。将它减少为3。:-)好的,这是正确的答案,所以我应该总是在线程内部使用循环,而不是在main()中使用循环?@EduardoG:不是“总是”-你应该在你想让你的计算机这样做的时候做。你能解释一下当循环
时线程如何从
退出吗?@AbhijitPritam:不会的。我在解释中说了。你应该总是提供一个完美的答案。这个程序永远不会结束,你必须杀死它。你的解释是不够的,这是错误的实际上,这是一个糟糕的答案。第二个join语句将永远不会执行。你们嘲笑我,说我对多线程一无所知。现在问问你们自己吧。