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

How to use threads correctly in C++

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use threads correctly for C++". In daily operation, I believe many people have doubts about how to use threads correctly for C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to use threads correctly by C++". Next, please follow the editor to study!

CP.26: do not detach threads

Reason (reason)

Typically, the requirement that the life cycle exceeds the thread that created it is inherited from the thread task, but the idea of detach makes it more difficult to monitor and communicate with the separate thread. In special cases, it is difficult (though not impossible) to ensure that the thread can abort or continue as expected.

Example (sample)

Void heartbeat ()

Void use ()

{

Std::thread t (heartbeat); / / don't join; heartbeat is meant to run forever

T.detach ()

/ /...

}

This code is a reasonable use of threads, where detach () is usually used. Although there are problems with this practice. How do we monitor a detached thread to know if it is active? Some problems may occur in the heartbeat thread, and the loss of heartbeat is a serious problem for systems that need heartbeat function. Therefore, we need to communicate with the heartbeat thread (for example, through an information flow or notification events using condition_variable).

An alternative, and usually superior solution is to control its lifetime by placing it in a scope outside its point of creation (or activation). For example:

Another optional, usually more advanced approach is to put it outside the scope where it was created (or activated). For example:

Void heartbeat ()

Gsl::joining_thread t (heartbeat); / / heartbeat is meant to run "forever"

This heartbeat will (barring error, hardware problems, etc.) Run for as long as the program does.

The heartbeat thread will run as long as the program (unless there are errors, hardware problems, etc.).

Sometimes, we need to separate the point of creation from the point of ownership:

Sometimes we need to separate the generation viewpoint from the ownership viewpoint.

Void heartbeat ()

Unique_ptr tick_tock {nullptr}

Void use ()

{

/ / heartbeat is meant to run as long as tick_tock lives

Tick_tock = make_unique (heartbeat)

/ /...

} Enforcement (implementation recommendations)

Flag detach ().

Tag detach (detach) operation.

At this point, the study on "how to use threads correctly in C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report