前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则CP.24:将线程看作全局​容器

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

作者头像
面向对象思考
发布2020-07-07 10:41:54
3490
发布2020-07-07 10:41:54
举报

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(示例)

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档