专栏首页C++核心准则原文翻译C++核心准则CP.24:将线程看作全局​容器

C++核心准则CP.24:将线程看作全局​容器

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<int>(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

本文分享自微信公众号 - 面向对象思考(OOThinkingDalian),作者:面向对象思考

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-07-05

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则:gsl::joining_thread好于std::thread

    A joining_thread is a thread that joins at the end of its scope. Detached thread...

    面向对象思考
  • C++核心准则C.120:类层次体系只用于表现固有的阶层结构‍

    C.120: Use class hierarchies to represent concepts with inherent hierarchical st...

    面向对象思考
  • C++核心准则ES.20: 保证所有对象被初始化

    Avoid used-before-set errors and their associated undefined behavior. Avoid prob...

    面向对象思考
  • HenCoder Android 自定义 View 1-6:属性动画(上手篇)

    如果你没听说过 HenCoder,可以先看看这个: HenCoder:给高级 Android 工程师的进阶手册

    扔物线
  • Java-ThreadPoolExecutor类

            handler:提交线程数量大于maximumPoolSize时的处理器

    尚浩宇
  • iOS-多线程死锁问题

    虽然作者在开发中经常会用到多线程,但是对于死锁、线程阻塞等问题还是比较模糊,一般都是先写吧,有问题再改,现在闲下来了,是时候总结一波了,本文主要总结一下同步/异...

    用户2215591
  • Android开发中基础动画技巧的应用 原

            我是先入门iOS的移动开发者,提到动画开发,iOS开发者很容易联想到3种方式,UIImageView的帧动画,UIView层的属性动画和Core...

    珲少
  • 我精心挑选了18本给0岁运营的书单。

    这18本运营相关的书籍, 到底值不值¥830? 看完肯定超值! ---- 《一胜九败》28 《史玉柱,我的营销心得》 42 《Facebook》 29 《参与感...

    mixlab
  • ReactNative之结合具体示例来看RN中的的Timing动画

    今天继续更新RN相关的博客。上篇博客详细的聊了RN中关于Flex布局的相关东西,具体请参见《ReactNative之参照具体示例来看RN中的FlexBox布局》...

    lizelu
  • java 中的同步机制

      对于有些场景,需要a、b线程按照顺序去执行,因为b线程要依赖a线程对某共享资源或 状态处理后,对于这种情况可以使用 private CountDownLat...

    杉枫

扫码关注云+社区

领取腾讯云代金券