From :http://blog.csdn.net/skyflying2012/article/details/24655751 and related forums
Http://blog.chinaunix.net/uid-29783732-id-4485673.html
In the Writing network server program may need to implement multi-threaded to receive data from multiple clients, I realize the way is silly, the dead loop waits for the client to create thread after connect,connect, so there is actually a problem, the server program needs long-running, long-time thread creation, Recycling of thread resources is a problem.
The thread resource of a program in a Linux system is limited, and it is shown that 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 the thread is repeatedly established in a program, and the thread exits by default, the final thread resource is exhausted and the process will no longer be able to establish a new thread.
To solve this problem, there are 2 ways that the system automatically frees the thread resource, or the thread resource is freed by another thread.
After the process runs itself, it is also a thread that threads the main thread, the main thread, and the main thread to create a shared process resource. Unlike other threads, after the main thread runs, the program exits and all the program-established threads exit.
Automatic release of a system
If you want the thread resources to be freed by the system when you wish to end the threads, you need to set the thread property to detach, which is the thread separation
Code, you can say this:
pthread_t Tid;
pthread_attr_t attr; Thread Properties
Pthread_attr_init (&ATTR); Initialize Thread properties
Pthread_attr_setdetachstate (&attr, pthread_create_detached); Set thread Properties
Pthread_create (&tid, &attr, Run, NULL); Build Thread
Two the resource is freed by another thread
Code, you can say this:
pthread_t T;
Pthread_create (null, NULL, Getandsaveauthviewsdrstub, (void*) LP);
Pthread_join (t);
Pthread_join (t) waits for thread t to exit and frees the resource occupied by the T thread.
The Pthread_join function blocks waiting for the specified thread to exit and then reclaims the resource, so that there is a synchronization feature that allows one thread to wait for another thread to exit before continuing, but this method is not good for server programs if the main thread is working on the newly created threads. You need to use method one
linux thread execution differs from Windows, Pthread has two states joinable state and unjoinable state, and if the thread is joinable state, the thread occupies the stack and the thread descriptor (total 8K) when the thread function returns to exit or Pthread_exit. These resources will be released only if you call Pthread_join.
If the thread of the unjoinable state, these resources are automatically freed when the threads function exits or pthread_exit.
The Unjoinable property can be specified at pthread_create time, or Pthread_detach itself in the thread after the thread is created, such as: Pthread_detach (Pthread_self ()), Change the status to Unjoinable state to ensure the release of the resource. or set the thread to joinable, and then call Pthread_join as appropriate.
There are also 2 functions for separating threads, Pthread_detach (ThreadID) and Pthread_detach (Pthread_self ()).
These 2 function differences are calling them Different threads, no other difference.
The function of the Pthread_detach (ThreadID) function is to leave threads with thread ID threadid in a detached state, and once the thread is in a detached state, the underlying resource is immediately recycled when the thread terminates Otherwise, the state of the terminating child thread is persisted (consumes system resources) until the main thread calls Pthread_join (Threadid,null) to get the exit status of the threads.
Usually the main thread uses pthread_create () to create a child thread, it is generally possible to call Pthread_detach (ThreadID) to detach the child thread just created, where the ThreadID is the threadid of the child thread; The underlying resource is immediately recycled when the child thread stops;
The child thread that is created can also detach itself, and the child thread calls Pthread_detach (Pthread_self) to detach itself, because the function returned by Pthread_self () is its own thread ID.
Note: in the case of detached attributes, a thread ends up releasing the system resources it occupies immediately, but one thing to note is that if you set a thread to detach a property and the thread runs very fast, it is likely to be in the Pthread_ The CREATE function terminates the run of the thread function before it terminates, and it is very likely that the thread number and system resources will be handed over to other threads for use, and the thread that calls pthread_create gets the wrong thread number.
Linux Thread Resource recycling