The difference between c ++ 11 thread join in and detach the

Thread Status:

The lifetime of a thread, may be converted between various states, different operating systems may implement different threading models, define a number of different threads states, each state may further comprise a plurality of sub-states, but generally speaking, It is common following several states:

1) Ready: Participation scheduling, waiting to be executed, once selected to be scheduled immediately begin

2) Run: occupy CPU, running in

3) Sleep: temporarily involved in scheduling, wait for a specific event occurs

4) suspension: it has finished running, waiting for resource recovery thread

Threaded environment:

Thread exists in the process, all global resources within the internal process for each thread is visible.

A typical global resources within the process as follows:

1) code area: This means that the current process within the space of all visible function code for each thread, is also visible

2) static memory: global variables, static space

3) dynamic memory: heap space

A typical thread of local resources:

1) Local stack space: storing the thread function call stack, local variables inside a function

2) partial register variables: the thread to be executed next pointer offset code

After a process initiated, will first create a default thread, this thread is usually called the main thread, C / C ++ program, the main thread is through the main function into the thread, the main thread becomes Cheng Yansheng from the thread, from thread you can also have their own entry function, the main function corresponding to the main thread, the function specified by the user. Thread pointer passed to the function achieved by the constructor, when specifying the thread entry function, the parameters of the entry function can also be specified. Like the main function have the same fixed format requirements, the thread function entry can also have a fixed format requirements, parameters are usually void type, return type is different depending on the protocol, the pthread is void , WINAPI is unsigned int, and we are all global functions.

The most common thread model, in addition to the main thread is special, other threads once created, is a peer relationship, implied hierarchical relationship does not exist between each other. The maximum number of threads per process can be achieved by the creation of specific decisions.

Whether in the windows or Posix, the default relationship between the main thread and the child thread is: whether the child thread is finished or not, once the main thread completes exit, all sub-thread execution will be terminated. Then the whole process ends or dead, to maintain a portion of the thread termination status execution but not yet destroyed, and the process must be destroyed after the destruction of all of its threads, then the process is in dead state. Thread function completes exit, terminate or otherwise very, terminate the thread into the state, but the thread is allocated for system resources are not necessarily release, possibly before the system is rebooted, has not been able to release, terminate the thread state, still as a thread entity exist in the operating system, and when destroyed, depending on the thread attributes. In this case, the main thread and the child thread is usually defined the following two relationships:

1, can join (joinable): In this relationship, the main thread needs to wait for a clear implementation of the operation, after the child thread, the main thread of execution wait operation is completed, the child thread and join the main thread, the main thread then wait for the operation to continue the next step after. The main thread must meet the child thread can rendezvous. In the main thread wait thread function calls an internal function object to achieve sub-thread, even if the child thread can be finished before the main thread, into the termination state, also joined the operation must be performed, otherwise, the system will never take the initiative to destroy threads, assigned to the system resources thread will never release.

2, phase separation (detached): represents the child thread and do not need to join the main thread, which is the phase separation, in this case, the child thread to terminate once into the state, this approach is more commonly used in the case of the number of threads, and sometimes let the main thread waits for the child by one end of the thread, the main thread or make arrangements for each child thread the end of the waiting order, it is difficult or impossible, so in a more complicated by sub-thread cases, this approach will often use.

At any one point in time, the thread is bound (the Joinable) or separable (detached), a binding thread to be recovered resources and kill other threads, another thread before being recycled, its resources, such as memory stack, is not released, on the contrary, a separate thread can not be recycled or kill another thread, which automatically releases the memory resources by the system when it terminates.

Separating a thread state of the thread determine in what way to terminate itself, in the case of default, the thread is non-isolated state, in this case, the original thread wait for the end of the thread is created only when pthread_join function returns, threads created considered terminated own release system resources occupied, and the thread is not separated from other threads waiting to run their own end, the thread will cease immediately release system resources.

Guess you like

Origin www.cnblogs.com/LuckCoder/p/10950583.html