Thread Exit
A single thread can exit 3 ways, so it can stop its control flow without terminating the entire process.
1 The thread can simply return from the boot process, and the return value is the exit code of the thread.
2 The thread can be canceled by other threads in the unified process.
3 thread calls Pthread_exit.
#include <pthread.h>
void pthread_exit (void *rval_ptr);
The rval_ptr parameter is a no type pointer.
Other threads in the process can also access the pointer by calling the Pthread_join function.
#include <pthread.h>
int pthread_join (pthread_t thread,void **rval_ptr);
Successfully returns 0, otherwise error number returned
when Pthread_join is invoked, the calling thread blocks until it calls Pthread_exit, returns from the startup routine, or is canceled.
If the thread simply returns from its startup routine, Rval_ptr will include the return code. If the thread is canceled, the memory unit specified by RVAL_PTR is set to pthread_canceled.
A thread can request to cancel other threads in the same process by calling the Pthread_cancel function.
#include <pthread.h>
int pthread_cancel (pthread_t tid);//Successful return 0, failure return error number
Pthread_cancel does not wait for the thread to terminate, it simply makes a request, and the thread can choose to ignore the cancellation or control how it is canceled. Thread Resource Release
The thread resources of a program in a Linux system are limited, and the number of threads that can run concurrently for a program is limited. By default, when a thread ends, its corresponding resource is not freed, so if a thread is repeatedly established in one program and the thread defaults, the end thread resource is exhausted and the process is no longer able to create a new thread.
Threads are divided into associative (joinable) and detached (detached), and if the thread's properties are not set to be pthread_create_detached when the thread is created, the thread defaults to be binding.
A thread that can be combined does not immediately release resources after it has exited, and must call Pthread_join to explicitly end the thread.
Separated threads automatically recycle resources when the thread exits. several ways to set up detach threads:
1. When creating a thread, add
pthread_attr_t attr;
pthread_t thread;
Pthread_attr_init (&attr);
Pthread_attr_setdetachstat (&attr, pthread_create_detached);
Pthread_create (&thread, &attr, &thread_function, NULL);
Pthread_attr_destroy (&ATTR);
2. Call Pthread_detach (Pthread_self ()) in the thread;
3. Calling Pthread_detach (PID) in the main thread, and the PID as the thread number of the child thread
Note that a thread that is set to detach cannot invoke Pthread_join, and an error occurs after the call. several ways to exit a binding thread
1. The child thread uses return exit and the Pthread_join recycle thread is used in the main thread.
2. The child thread exits using Pthread_exit, the main thread uses Pthread_join to receive the Pthread_exit return value, and reclaims the thread.
3. Call the Pthread_cancel in the main thread and call the Pthread_join recycle thread.
Reference:
1. Advanced Programming of UNIX environment-thread termination
2.http://blog.chinaunix.net/uid-29924858-id-4603600.html