分离调用未按预期工作
detach call is not working as per expectation
需要帮助了解以下代码工作方式不同的原因。
在此代码线程中,主退出后不打印任何内容:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void Run()
{
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
}
int main()
{
std::thread th(Run);
th.detach();
std::this_thread::sleep_for(std::chrono::seconds(2));
cout<<"==== Ending main ==="<<std::endl;
}
但是线程退出后会打印正确的消息:
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
应该是个很小的东西,我没看到。
编译器信息:
g++/Mac/c++11
曾尝试在 google 上进行搜索,但运气不好。
编辑-1:
第一个程序的结果:
=== start_point ===
=== start_point ===
==== Ending main ===
第二个项目的结果:
Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.
我认为该代码执行正确。
一般在thread
的析构函数运行s时,会等待线程执行完毕后退出。但是,当您调用 detach()
时,您正在规避此行为 - 当前方法不会等待线程完成。
由于线程将 运行(大约)6 秒,main()
中的休眠等待威胁完成的时间不够长,因此它会在线程完成之前退出.根据环境的不同,退出 main()
也可能会退出程序 - 这意味着线程在程序退出之前没有机会打印所有消息。
如果你想在继续执行前显式地等待线程完成,你应该调用thread::join()
。
需要帮助了解以下代码工作方式不同的原因。 在此代码线程中,主退出后不打印任何内容:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void Run()
{
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"=== "<< "start_point" <<" ==="<<endl;
}
int main()
{
std::thread th(Run);
th.detach();
std::this_thread::sleep_for(std::chrono::seconds(2));
cout<<"==== Ending main ==="<<std::endl;
}
但是线程退出后会打印正确的消息:
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
应该是个很小的东西,我没看到。 编译器信息: g++/Mac/c++11
曾尝试在 google 上进行搜索,但运气不好。
编辑-1: 第一个程序的结果:
=== start_point ===
=== start_point ===
==== Ending main ===
第二个项目的结果:
Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.
我认为该代码执行正确。
一般在thread
的析构函数运行s时,会等待线程执行完毕后退出。但是,当您调用 detach()
时,您正在规避此行为 - 当前方法不会等待线程完成。
由于线程将 运行(大约)6 秒,main()
中的休眠等待威胁完成的时间不够长,因此它会在线程完成之前退出.根据环境的不同,退出 main()
也可能会退出程序 - 这意味着线程在程序退出之前没有机会打印所有消息。
如果你想在继续执行前显式地等待线程完成,你应该调用thread::join()
。