代码:
void *PrintHello(void *threadid)
{
cout<<"Hello"<<endl;
sleep(3);
cout<<"Still PrintHello is alive"<<endl;
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
cout<<"Calling thread:"<<t<<endl;
pthread_create(&threads[0], NULL, PrintHello, NULL);
//pthread_join(threads[0],NULL);
cout<<"Main exits"<<endl;
pthread_exit(NULL);
}
为什么这里的行为像是?也就是说,为什么要退出cc,而不是破坏并允许它继续?
最佳答案
pthread_exit()
只终止调用线程。因此,当您从main()
调用它时,它会终止主线程,同时允许进程继续。这是意料之中的。
如果您调用exit()
(或者返回的隐式退出),它将终止整个过程,并且您也会看到printHello
终止。