Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between join and detach, the thread libraries in C++?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the difference between join and detach in thread library in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Thread state

In the lifetime of a thread, it can switch between multiple states, different operating systems can implement different thread models, define many thread states, and each state can contain multiple sub-states. But in general, several states of all operating systems are common.

Ready: participate in scheduling, wait for execution, and start execution as soon as it is selected by CPU.

Running state: occupies CPU and is running.

Dormant state: do not participate in scheduling temporarily, wait for a specific event to occur and then turn into a ready state.

Halted state: has finished running, waiting for thread resources to be reclaimed.

Thread environment

Threads exist in the process, and all global resources within the process are visible to each thread within the process.

Typical global resources (thread-visible resources) in a process are as follows:

Code area: means that all function code in the current process space is visible to each thread.

Static storage: global variables, accessible static variables.

Dynamic storage: dynamically generated variables (new).

Typical local resources in a thread are as follows:

1. Local stack space: the function call stack where the thread is stored, the local variables inside the function, etc.

two。 Partial register variable: the pointer offset of the code to be executed by the thread next.

The difference between join and detach

After a process is initiated, it will first generate a default thread, which is usually called the master thread. In the C _ main + program, the master thread enters the thread through the master thread. The thread derived from the master thread becomes a slave thread (also known as a child thread). The slave thread can also have its own entry function, which is equivalent to the main function of the master thread, which is specified by the user. Thread is typically used to create child threads. By passing a function pointer in the thread constructor, you can also specify the parameters of the entry function when you specify the thread entry function.

In the most common thread model, except for the special main thread, once other threads are created, they are peer-to-peer and there is no implicit hierarchical relationship. The maximum number of threads that can be created per process is determined by the specific implementation.

Whether in windows or Posix, the default relationship between the main thread and the child thread is: no matter whether the child thread completes execution or not, once the main thread exits, all child thread execution will be terminated. Some threads remain in a state that terminates execution but has not yet been destroyed, and the process must be destroyed after all its threads are destroyed, when the entire process is in a zombie state (which may cause the program to crash).

In this case, the main thread and child threads usually define the following two relationships:

1. Joinable: in this relationship, the main thread needs to explicitly perform the waiting operation. After the child thread ends, the waiting operation of the main thread completes, and the child thread and the main thread rendezvous. Then the main thread continues to perform the next step after the waiting operation. The main thread must meet the rendezvous child threads. If the wait function of the child thread object is called inside the thread function of the main thread, even if the child thread can finish execution before the main thread and enter the termination state, the rendezvous operation must be performed, otherwise, the system will never take the initiative to destroy the thread and the system resources allocated to the thread will never be released.

2. Detached: in this relationship, the child thread does not need to rendezvous with the main thread, that is, it is separated. In this case, once the child thread enters the termination state, this method is often used in the case of a large number of threads. Sometimes it is difficult or impossible for the main thread to wait for the child thread to end one by one or to arrange the waiting order for the end of each child thread, so this method is often used when there are more concurrent child threads.

This is the end of the content of "what is the difference between join and detach in the thread library of C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report