std string与线程安全_C++核心准则CP.24:将线程看作全局?容器

8018ab7cb1229441e9c16fdc4522c8f8.png

CP.24: Think of a thread as a global container

CP.24:将线程看作全局容器

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).

为了维持指针的安全性并避免泄露。我们需要考虑线程使用了什么指针。如果线程被detach了,我们可以(只能)安全地向线程传递指向静态变量和自由存储对象的指针。

Example(示例)

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();    // ...}

By "OK" we mean that the object will be in scope ("live") for as long as a thread can use the pointers to it. By "bad" we mean that a thread may use a pointer after the pointed-to object is destroyed. The fact that threads run concurrently doesn't affect the lifetime or ownership issues here; these threads can be seen as just a function object called from some_fct.

通过”OK“这个词我们想表达的是只要线程继续使用某个指针,该指针指向的对象就会留在范围内(并保持可用状态)。通过“bad”这个词,我们想表达的是线程会在对象销毁之后使用指向这个对象的指针。这里,线程并发执行这个事实不会影响生命周期和所有权话题;可以认为这些线程只是some_fct调用的函数对象。

Note(注意)

Even objects with static storage duration can be problematic if used from detached threads: if the thread continues until the end of the program, it might be running concurrently with the destruction of objects with static storage duration, and thus accesses to such objects might race.

如果被已经detach了的线程使用的话,哪怕具有静态存储期间的对象也会发生问题:如果该线程一直执行到程序结束,它可能和具有静态存储期间的对象的析构过程并发执行,对于这样的对象的访问可能发生竞争。

Note(注意)

This rule is redundant if you don't detach() and use gsl::joining_thread. However, converting code to follow those guidelines could be difficult and even impossible for third-party libraries. In such cases, the rule becomes essential for lifetime safety and type safety.

如果你不会detach线程并且使用gsl::joining_thread,本准则就是多余的。然而,转换代码以遵守该准则会很困难,如果是第三方库可能根本就无法实现。在这种情况下,为了保证生命周期安全和类型安全,本准则就变得非常有必要。

In general, it is undecidable whether a detach() is executed for a thread, but simple common cases are easily detected. If we cannot prove that a thread does not detach(), we must assume that it does and that it outlives the scope in which it was constructed; After that, the usual lifetime and ownership (for global objects) enforcement applies.

通常,无法判断某个线程是否会执行detach操作,但在简单的常见情况时容易检测。如果我们无法证明线程不会调用detach,我们必须假设它会调用并且它的生存期间会超过它被构造的范围;接下来就可以适用通常的生命周期和所有权建议了。

Enforcement(实施建议)

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

标记企图将局部变量传递给可能detach的线程的情况。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp24-think-of-a-thread-as-a-global-container


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值