首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则CP.1: 设想你的代码​会成为多线程程序的一部分

C++核心准则CP.1: 设想你的代码​会成为多线程程序的一部分

作者头像
面向对象思考
发布2020-07-02 15:32:59
3270
发布2020-07-02 15:32:59
举报

CP.1: Assume that your code will run as part of a multi-threaded program

CP.1: 设想你的代码会成为多线程程序的一部分

Reason(原因)

It's hard to be certain that concurrency isn't used now or won't be used sometime in the future. Code gets reused. Libraries not using threads may be used from some other part of a program that does use threads. Note that this rule applies most urgently to library code and least urgently to stand-alone applications. However, over time, code fragments can turn up in unexpected places.

很难确定现在不需要并发或者将来的某个时间也不会使用。代码会被重用的。没有使用线程的库有可能被使用线程的、程序的其它部分使用。注意本准则对于功能库具有最大的紧迫性,而对于单独的应用程序就没什么紧迫性。然而,随着时间的推移,代码片段会出现在意想不到的地方。

Example, bad(反面示例)

double cached_computation(double x)
{
    // bad: these statics cause data races in multi-threaded usage
    static double cached_x = 0.0;
    static double cached_result = COMPUTATION_OF_ZERO;

    if (cached_x != x) {
        cached_x = x;
        cached_result = computation(x);
    }
    return cached_result;
}

Although cached_computation works perfectly in a single-threaded environment, in a multi-threaded environment the two static variables result in data races and thus undefined behavior.

虽然缓存计算可以在单线程环境中运行得很完美,但在多线程环境中,两个静态变量处于数据竞争状态,因此会导致无定义的行为。

Example, good(范例)

struct ComputationCache {
    double cached_x = 0.0;
    double cached_result = COMPUTATION_OF_ZERO;

    double compute(double x) {
        if (cached_x != x) {
            cached_x = x;
            cached_result = computation(x);
        }
        return cached_result;
    }
};

Here the cache is stored as member data of a ComputationCache object, rather than as shared static state. This refactoring essentially delegates the concern upward to the caller: a single-threaded program might still choose to have one global ComputationCache, while a multi-threaded program might have one ComputationCache instance per thread, or one per "context" for any definition of "context." The refactored function no longer attempts to manage the allocation of cached_x. In that sense, this is an application of the Single Responsibility Principle.

这段代码中,缓存数据存作为ComputationCache对象的数据成员存储,而不是静态的状态数据。这个重构从根本上将决定权向上委托给调用者:单线程程序可以继续使用全局的ComputationCache实例,而多线程程序可以每个线程管理一个ComputationCache实例,或者每个上下文一个实例,这个上下文可以有多种解释。重构的函数不再试图管理cached_x的内存分配。从这个角度来讲,这算是单独责任原则的一个应用。

In this specific example, refactoring for thread-safety also improved reusability in single-threaded programs. It's not hard to imagine that a single-threaded program might want two ComputationCache instances for use in different parts of the program, without having them overwrite each other's cached data.

在这个特定的例子中,目的在于线程安全的重构同时提高了单线程环境中的重用性。不难想象单线程有可能需要两个用于程序不同部分的ComputationCache实例,而不会发生缓存数据的相互覆盖。

There are several other ways one might add thread-safety to code written for a standard multi-threaded environment (that is, one where the only form of concurrency is std::thread):

存在很多其他的方式,其中一个是为了在标准的多线程环境(即,使用并发的唯一形式std::thread)中运行的代码中增加线程安全处理。

  • Mark the state variables as thread_local instead of static.
  • 将状态变量定义为线程内部的局部变量而不是静态变量。
  • Implement concurrency control, for example, protecting access to the two static variables with a static std::mutex.
  • 实现并发控制,例如,使用静态的std::mutex保护对两个静态变量的访问。
  • Refuse to build and/or run in a multi-threaded environment.
  • 拒绝在多任务环境中编译或执行代码。
  • Provide two implementations: one for single-threaded environments and another for multi-threaded environments.
  • 提供两种实现:一个用于单线程环境,另一个用于多线程环境。

Exception(例外)

Code that is never run in a multi-threaded environment.

永远不会运行于多线程环境的代码。

Be careful: there are many examples where code that was "known" to never run in a multi-threaded program was run as part of a multi-threaded program, often years later. Typically, such programs lead to a painful effort to remove data races. Therefore, code that is never intended to run in a multi-threaded environment should be clearly labeled as such and ideally come with compile or run-time enforcement mechanisms to catch those usage bugs early.

需要小心的是:存在很多事例本来被认为永远不会运行于多线程程序的代码最后成为多线程程序的一部分,通常是几年之后。一般来讲,为这样的程序消除数据竞争会非常痛苦。因此,一旦决定代码永远不会在多线程环境中运行,应该清晰地标记出来,而且最好同时提供编译和运行时的强制机制以尽早捕捉错误的用法。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp1-assume-that-your-code-will-run-as-part-of-a-multi-threaded-program

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CP.1: Assume that your code will run as part of a multi-threaded program
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档