简体   繁体   中英

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

pthread_exit when placed in main() before 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. 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. Hence the question.

OS: Linux

Returning from main() with a return statement is equivalent to calling exit() --- it terminates the process, without waiting for other threads to finish.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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