[Linux learning] pthread_create main thread and new thread exit relationship

Source: Internet
Author: User

We usually create another new thread in one thread. Will the new thread be affected if the main thread exits? Next we will discuss it.

 

1. The main thread waits for the new thread to end and exit. Normal execution.

Instance code:

# Include "apue. H "# include <pthread. h> pthread_t ntid; // thread idvoid printids (const char * s) {pid_t PID; pthread_t tid; pid = getpid (); tid = pthread_self (); printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID, (unsigned INT) tid, (unsigned INT) TID);} void * thrfun (void * Arg) {// sleep (1); // enable the winner thread to exit printids ("New thread") first "); return (void *) 0);} int main () {int err; err = pthread_create (& ntid, null, thrfun, null ); If (Err! = 0) err_quit ("can't create thread: % s \ n", strerror (ERR); printids ("main thread"); sleep (1 ); // wait until the new thread ends exit (0 );}

Running result:

2. The process exits first, and the new thread exits immediately. The system clears all resources.

Instance code:

# Include "apue. H "# include <pthread. h> pthread_t ntid; // thread idvoid printids (const char * s) {pid_t PID; pthread_t tid; pid = getpid (); tid = pthread_self (); printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID, (unsigned INT) tid, (unsigned INT) TID);} void * thrfun (void * Arg) {sleep (1); // enable the winner thread to exit printids ("New thread") first; Return (void *) 0);} int main () {int err; err = pthread_create (& ntid, null, thrfun, null ); If (Err! = 0) err_quit ("can't create thread: % s \ n", strerror (ERR); printids ("main thread"); // sleep (1 ); exit (0); // note that the process (not the thread) Exits}

Running result:

The new thread created after the main thread exits also stops running.

3. If the main thread calls pthread_exit, it exits and the sub-thread does not exit.

Instance code:

# Include "apue. H "# include <pthread. h> pthread_t ntid; // thread idvoid printids (const char * s) {pid_t PID; pthread_t tid; pid = getpid (); tid = pthread_self (); printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID, (unsigned INT) tid, (unsigned INT) TID);} void * thrfun (void * Arg) {sleep (1); // enable the winner thread to exit printids ("New thread") first; Return (void *) 0);} int main () {int err; err = pthread_create (& ntid, null, thrfun, null ); If (Err! = 0) err_quit ("can't create thread: % s \ n", strerror (ERR); printids ("main thread"); // sleep (1 ); pthread_exit (null); exit (0 );}

Running result:

POSIX standard definition:

When you program with POSIX Threads API, there is one thing about pthread_exit () that you may ignore for mistake. insubroutines that complete normally, There is nothing special you have to dounless you want to pass a return code back using pthread_exit ().
The completionwon't affect the other threads which were created by the main thread of thissubroutine. however, in Main (), when the code has been executed to the end, there cocould leave a choice for you. if you want to kill all the threads thatmain () created
Before, you can dispense with calling any functions. but if you want to keep the process and all the other threadsexcept for the main thread alive after the exit of main (), then you can call pthread_exit () to realize it. and any files
Opened inside the main thread will remain openafter its termination.

 

According to the POSIX standard definition, when the main thread calls pthread_exit () before the sub-thread ends, the sub-thread will not exit.

 

Note: Calling pthread_exit () in the main function only exits the main thread, but the process does not exit. Therefore, the new thread continues execution without exiting.

We can add an output statement printf ("mainthread has exited! \ N ");. The output result is not changed, indicating that this statement is not executed. This means that the process has not exited.

 

Therefore:

The exit of one thread does not affect another thread. However, when the process ends, all threads end and all resources are recycled.

 

We can write another program for verification:

4. Create a New thread C again in the created new thread B. If B exits first, C will continue to run without exiting.

Instance code:

# Include "apue. H "# include <pthread. h> pthread_t ntid; // thread idvoid printids (const char * s) {pid_t PID; pthread_t tid; pid = getpid (); tid = pthread_self (); printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID, (unsigned INT) tid, (unsigned INT) TID);} void * thrfun2 (void * Arg) {sleep (1); // causes the main thread that creates it to exit printids ("New thread of the new thread") first "); return (void *) 0);} void * thrfun (void * Arg) {sleep (1); // causes the winner thread to exit first Printids ("New thread"); int err; err = pthread_create (& ntid, null, thrfun2, null); If (Err! = 0) err_quit ("Can 'tcreate thread: % s \ n", strerror (ERR); Return (void *) 0);} int main () {int err; err = pthread_create (& ntid, null, thrfun, null); If (Err! = 0) err_quit ("Can 'tcreate thread: % s \ n", strerror (ERR); printids ("main thread"); // sleep (1 ); pthread_exit (null); printf ("main thread has exited! \ N "); exit (0 );}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.