不能在线程函数中调用其他函数在终止该线程,以下代码的用法是错误的:
std::thread* pThread;
void fun2()
{
if (pThread->joinable())
{
std::cout << pThread->get_id() << std::endl;
pThread->join();
delete pThread;
pThread = NULL;
printf("fun2 is ending\n");
}
}
void fun1()
{
printf("this is in fun1\n");
fun2();
}
int main()
{
int i = 0;
pThread = new std::thread(fun1);
Sleep(2000);
printf("main is ending\n");
return 0;
}
代码会出现的错误如下:
if (_Thrd_join(_Thr, 0) != _Thrd_success) // 线程结合(_Thrd_join()是join方法的核心),是阻塞的
_Throw_Cpp_error(_NO_SUCH_PROCESS);
错误原因说明:在线程函数中调用另外一个函数,而该函数在通