简体   繁体   English

分离的线程

[英]Detached Threads

When we make Detached threads in main. 当我们在main中创建Detached线程时。 and supose main exits... do the detached threads keep on going on or do they also exit just like our normal Joinable threads? 并且主要的出口......分离的线程继续进行还是像普通的Joinable线程一样退出?

It depends entirely on how the main thread exits. 它完全取决于主线程如何退出。 If it exits using exit() or returning from main() , then the entire process is exited, and every thread is terminated. 如果它使用exit()或从main()返回,则退出整个进程,并终止每个线程。

However, if it uses pthread_exit() to terminate, then the process continues running. 但是,如果它使用pthread_exit()来终止,则该过程继续运行。

If this would be another thread then main, the other threads would continue. 如果这是另一个线程然后是主线程,其他线程将继续。 But the C99 standard says 但C99标准说

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function... 如果main函数的返回类型是与int兼容的类型,则从初始调用返回main函数等同于调用exit函数...

(All common platforms nowadays will return an int from main , in particular this is required by POSIX.) (现在所有常见平台都会从main返回一个int ,特别是POSIX需要这个。)

And the POSIX page for exit states 以及exit状态的POSIX页面

These functions shall terminate the calling process... 这些功能应终止呼叫过程......

So in summary a return from main terminates the whole program including all threads. 总而言之,从main返回终止整个程序,包括所有线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM