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 does C++ treat threads as global ​ containers

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how C++ treats threads as global containers". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how C++ treats threads as global containers".

CP.24: treat threads as global containers

Reason (reason)

To maintain pointer safety and avoid leaks, we need to consider what pointers are used by a thread. If a thread is detached, we can safely pass pointers to static and free store objects (only).

To maintain the security of the pointer and avoid disclosure. We need to consider what pointers the thread uses. If a thread is detach, we can safely pass pointers to static variables and free storage objects to the thread.

Example (sample)

Void f (int* p)

{

/ /...

* p = 99

/ /...

}

Int glob = 33

Void some_fct (int* p)

{

Int x = 77

Std::thread T0 (f, & x); / / bad

Std::thread T1 (f, p); / / bad

Std::thread T2 (f, & glob); / / OK

Auto Q = make_unique (99)

Std::thread T3 (f, q.get ()); / / bad

/ /...

T0.detach ()

T1.detach ()

T2.detach ()

T3.detach ()

/ /...

}

What we mean by the word "OK" is that as long as the thread continues to use a pointer, the object that the pointer points to will remain in range (and remain available). Through the word "bad", what we want to say is that the thread will use a pointer to the object after it has been destroyed. Here, the fact that threads execute concurrently does not affect lifecycle and ownership topics; you can think of these threads as just function objects called by some_fct.

Note (Note)

Even objects with static storage periods can have problems if used by detach threads: if the thread executes until the end of the program, it may execute concurrently with the destructing process of objects with static storage periods, and access to such objects may compete.

Note (Note)

If you can't detach threads and use gsl::joining_thread, this rule is superfluous. However, it can be difficult to convert the code to comply with this guideline, and if it is a third-party library, it may not be implemented at all. In this case, in order to ensure life cycle safety and type safety, this guideline becomes very necessary.

In general, it is impossible to determine whether a thread will perform a detach operation, but it is easy to detect in simple and common situations. If we cannot prove that the thread will not call detach, we must assume that it will be called and that its lifetime will exceed the scope of its construction; then the usual life cycle and ownership recommendations can be applied.

Enforcement (implementation recommendations)

Flag attempts to pass local variables to a thread that might detach ().

Marks an attempt to pass a local variable to a thread that might detach.

At this point, I believe you have a deeper understanding of "how C++ treats threads as global containers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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