简体   繁体   English

pthread_exit如何在完成分配的任务之前阻止线程退出?

[英]How does pthread_exit stop a thread from quitting before completion of its assigned task?

pthread_exit when placed in main() before return 0; pthread_exit ,在return 0;之前放在main()中return 0; does stop a thread from quitting before completion of its assigned task. 确保在完成分配的任务之前停止线程退出。

I wish to understand the reasons in detail . 我想详细了解原因。

I put a while loop in the function that the thread was supposed to work on. 我在线程应该使用的函数中放置了一个while循环。 The condition in the while didn't satisfy and yet the program terminated. 当时的情况不满足,但程序终止了。 When I put the pthread_exit in the main before return 0, the while loop completed its task. 当我在返回0之前将pthread_exit放入主线程时,while循环完成了其任务。 Hence the question. 因此是一个问题。

OS: Linux 操作系统:Linux

Returning from main() with a return statement is equivalent to calling exit() --- it terminates the process, without waiting for other threads to finish. 使用return语句从main()返回等同于调用exit() ---它终止进程,而无需等待其他线程完成。

Calling pthread_exit() just exits the thread that calls it (even if that thread is the one running main() ), so other threads will keep running until either some thread calls exit() (or another function that terminates the process, such as abort() ), or every thread has exited. 调用pthread_exit()只会退出调用它的线程(即使该线程是正在运行的main() ),因此其他线程将继续运行,直到某个线程调用exit() (或另一个终止进程的函数,例如abort() ),或者每个线程都已退出。

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

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