专栏首页C++核心准则原文翻译C++核心准则CP.4:按照任务思考问题,而不是线程

C++核心准则CP.4:按照任务思考问题,而不是线程

CP.4: Think in terms of tasks, rather than threads

CP.4:按照任务思考问题,而不是线程

Reason(原因)

A thread is an implementation concept, a way of thinking about the machine. A task is an application notion, something you'd like to do, preferably concurrently with other tasks. Application concepts are easier to reason about.

线程是实现层面的概念,一种理解机器动作的方式。任务是应用层面的观念,你希望它可以和其他任务并发执行。应用概念更容易理解。

Example(示例)

void some_fun()
{
    std::string msg, msg2;
    std::thread publisher([&] { msg = "Hello"; });       // bad: less expressive
                                                         //      and more error-prone
    auto pubtask = std::async([&] { msg2 = "Hello"; });  // OK
    // ...
    publisher.join();
}

Note(注意)

With the exception of async(), the standard-library facilities are low-level, machine-oriented, threads-and-lock level. This is a necessary foundation, but we have to try to raise the level of abstraction: for productivity, for reliability, and for performance. This is a potent argument for using higher level, more applications-oriented libraries (if possibly, built on top of standard-library facilities).

除了async()以外,标准库功能都是低层次,面向机器,线程/锁层次的。这些作为基础有必要,但是我们必须努力提高抽象的层次:为了生产性,为了可靠性,也为了性能。这是一个事关使用更高层次,更加面向应用的库的具有重大影响的话题(如果可能,将其构建在标注库功能的顶层)。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp4-think-in-terms-of-tasks-rather-than-threads

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

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

原始发表时间:2020-06-28

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

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好

    Readability. Direct expression of an idea.

    面向对象思考
  • C++核心准则E.25:如果不能抛出异常,模仿RAII方式进行资源管理

    Even without exceptions, RAII is usually the best and most systematic way of dea...

    面向对象思考
  • C++核心准则CP.31:在线程之间以传值方式传递少量数据,而不是传递引用或指针

    Copying a small amount of data is cheaper to copy and access than to share it us...

    面向对象思考
  • SAP UI5应用DatePicker控件的设计明细

    Recently in order to resolve customer incidents, I need to study more details ab...

    Jerry Wang
  • 【UVA 401】BUPT 2015 newbie practice #2 div2-B-Palindromes

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/B

    饶文津
  • 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D

    饶文津
  • 扩展多重旅行商问题以安排无人驾驶飞机执行监视任务(CS AI)

    在本文中,我们安排了一组无人机在整个图中的行进路径,在该图中需要在预定义的时间点多次访问节点。这是著名的多次旅行商问题(Traveling Salesman P...

    小童
  • 4.1 数值积分、高等函数绘制

    is defined informally as the signed area of the region in the xy-plane that is ...

    周星星9527
  • hdu1028(dp 或母函数)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth...

    用户2965768
  • HDUOJ---Piggy-Bank

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K...

    Gxjun

扫码关注云+社区

领取腾讯云代金券