从main调用pthread_exit可以吗?

当我从main调用pthread_exit时,程序永远不会终止。 我期望程序完成,因为我退出程序的唯一线程,但它不起作用。 好像挂了。

 #include  #include  #include  int main(int argc, char *argv[]) { printf("-one-\n"); pthread_exit(NULL); printf("-two-\n"); } 

Process Explorer显示(仅)线程处于Wait:DelayExecution状态。

根据pthread_exit文档:

在最后一个线程终止后,进程将以退出状态0退出。 行为应该就像实现在线程终止时调用带有零参数的exit()一样。

我正在使用Dev-C ++ v4.9.9.2pthreads-win32 v2.8.0.0 (链接libpthreadGC2.a )。

该库似乎没问题(例如,从main调用pthread_selfpthread_create可以了)。

有什么理由说我不应该从main调用pthread_exit吗?

那么它在pthreads的linux实现中肯定是合法的,请参阅pthreads_exit中的notes部分。 它指出

要允许其他线程继续执行,主线程应该通过调用pthread_exit()而不是exit(3)来终止。

此外,在这里查看源代码(扭转末尾)表明它大致转换为_endthread或_endthreadex。 这里的文档没有提到在初始线程中没有调用它。

这完全合法和预期的行为。 整个过程仅在所有线程终止或显式或隐式调用时结束。

main的正常返回相当于exit调用。 如果你使用pthread_exit结束main ,那么你明确表示你希望其他线程继续。

在main中使用pthread_exit很好。 当使用pthread_exit时,主线程将停止执行并保持僵尸(defunct)状态,直到所有其他线程退出。

如果在主线程中使用pthread_exit,则无法获取其他线程的返回状态,也无法对其他线程进行清理(可以使用pthread_join(3)完成)。 此外,最好分离线程(pthread_detach(3)),以便在线程终止时自动释放线程资源。 在所有线程退出之前,不会释放共享资源。

在主线程中不分配资源时可以使用,不需要清理。 下面的代码显示了在主线程中使用pthread_exit。 调用pthread_exit后,main中的第二个printf不会作为主线程退出。 Ps输出显示已失效的主线程。

  #include  #include  #include  #include  #include  void *functionC(void *); int main() { int rc; pthread_t th; if(rc = pthread_create(&th, NULL, &functionC, NULL)) { printf("Thread creation failed, return code %d, errno %d", rc, errno); } printf("Main thread %lu: Sleeping for 20 seconds\n", pthread_self()); fflush(stdout); sleep(20); pthread_exit(NULL); printf("Main thread %lu: This will not be printed as we already called pthread_exit\n", pthread_self()); exit(0); } void *functionC(void *) { printf("Thread %lu: Sleeping for 20 second\n", pthread_self()); sleep(20); printf("Thread %lu: Came out of first and sleeping again\n", pthread_self()); sleep(20); printf("CThread %lu: Came out of second sleep\n", pthread_self()); } // Output of the above code Main thread 140166909204288: Sleeping for 20 seconds Thread 140166900684544: Sleeping for 20 second Thread 140166900684544: Came out of first and sleeping again CThread 140166900684544: Came out of second sleep // ps output root@xxxx-VirtualBox:~/pthread_tst# ps -elfT |grep a.out 0 S root 9530 9530 9496 0 80 0 - 3722 hrtime 17:31 pts/1 00:00:00 ./a.out 1 S root 9530 9531 9496 0 80 0 - 3722 hrtime 17:31 pts/1 00:00:00 ./a.out 0 S root 9537 9537 2182 0 80 0 - 5384 pipe_w 17:31 pts/0 00:00:00 grep --color=auto a.out root@xxxx-VirtualBox:~/pthread_tst# ps -elfT |grep a.out 0 Z root 9530 9530 9496 0 80 0 - 0 - 17:31 pts/1 00:00:00 [a.out]  1 S root 9530 9531 9496 0 80 0 - 4258 hrtime 17:31 pts/1 00:00:00 ./a.out 0 S root 9539 9539 2182 0 80 0 - 5384 pipe_w 17:31 pts/0 00:00:00 grep --color=auto a.out` 

有关线程的更多信息,请查看博客Tech Easy 。

在Linux上测试时(CentOS Linux版本7.2.1511(核心版))我发现主程序确实等待“子”线程继续。 此外,我无法从main传递返回代码,虽然它可以指定为pthread_exit()的参数,因为Raul上面说它总是以退出代码0返回:

 retval=3; pthread_exit(&retval); 

我们还在使用Clang编译器(版本3.4.2)和清理程序选项时发现了一条错误消息:

 ==5811==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x7f4c090321d0 in thread T0 #0 0x7f4c08be3e29 in __interceptor_free (/home/karstenburger/tests/libc/pthread_exit_in_main/a+0x65e29) #1 0x7f4c08333358 in free_key_mem (/lib64/libdl.so.2+0x1358) #2 0x7f4c08745bc1 in __nptl_deallocate_tsd (/lib64/libpthread.so.0+0x7bc1) #3 0x7f4c07771b38 in __libc_start_main (/lib64/libc.so.6+0x21b38) #4 0x7f4c08bfa08c in _start (/home/karstenburger/tests/libc/pthread_exit_in_main/a+0x7c08c) AddressSanitizer can not describe address in more detail (wild memory access suspected). SUMMARY: AddressSanitizer: bad-free ??:0 __interceptor_free ==5811==ABORTING