相关文章推荐
逆袭的创口贴  ·  【历史沿革】毛泽东祖先毛太华生平_永胜县概况 ...·  1 年前    · 
不爱学习的电梯  ·  苹果慌了:美国反垄断法案持续推进,或强制苹果 ...·  2 年前    · 
犯傻的毛衣  ·  僵尸娘美图 - 抖音·  2 年前    · 
不要命的西装  ·  思维链(Chain-of-thoughts) ...·  2 年前    · 
酷酷的卡布奇诺  ·  为什么没人黑宝骏KiWi ...·  2 年前    · 
Code  ›  C++核心准则:gsl::joining_thread好于std::thread开发者社区
gsl
https://cloud.tencent.com/developer/article/1656650
英勇无比的瀑布
2 年前
作者头像
面向对象思考
0 篇文章

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

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > C++核心准则原文翻译 > C++核心准则:gsl::joining_thread好于std::thread

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

作者头像
面向对象思考
发布 于 2020-07-07 10:41:08
473 0
发布 于 2020-07-07 10:41:08
举报

CP.25: Prefer gsl::joining_thread over std::thread

CP.25: gsl::joining_thread好于std::thread

Reason(原因)

A joining_thread is a thread that joins at the end of its scope. Detached threads are hard to monitor. It is harder to ensure absence of errors in detached threads (and potentially detached threads).

joining_thread是一种在和作用域连结的线程。分离之后的线程很难监控。很难保证分离之后(或者存在潜在的分离可能性)的线程中不存在错误。

Example, bad(反面示例)

void f() { std::cout << "Hello "; }
struct F {
    void operator()() const { std::cout << "parallel world "; }
int main()
    std::thread t1{f};      // f() executes in separate thread
    std::thread t2{F()};    // F()() executes in separate thread
}  // spot the bugs
Example(示例)
void f() { std::cout << "Hello "; }
struct F {
    void operator()() const { std::cout << "parallel world "; }
int main()
    std::thread t1{f};      // f() executes in separate thread
    std::thread t2{F()};    // F()() executes in separate thread
    t1.join();
 
推荐文章
逆袭的创口贴  ·  【历史沿革】毛泽东祖先毛太华生平_永胜县概况_永胜县人民政府
1 年前
不爱学习的电梯  ·  苹果慌了:美国反垄断法案持续推进,或强制苹果开放iOS非商店下载 | 游戏大观 | GameLook.com.cn
2 年前
犯傻的毛衣  ·  僵尸娘美图 - 抖音
2 年前
不要命的西装  ·  思维链(Chain-of-thoughts)作为提示 - 知乎
2 年前
酷酷的卡布奇诺  ·  为什么没人黑宝骏KiWi EV?宝骏KiWi EV垃圾吗-无敌电动
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号