Loser-HomeWork icon indicating copy to clipboard operation
Loser-HomeWork copied to clipboard

关于 `C++CoreGuidelines` 对于 `detach` 的描述

Open Mq-b opened this issue 8 months ago • 19 comments

https://github.com/Mq-b/Loser-HomeWork/blob/main/C++CoreGuidelines解析/第4章-函数.md#f53-在非局部使用包括要被返回存储在堆上或要传给其他线程的-lambda-表达式中避免通过引用来捕获

std::cout 的生存期与进程的生存期绑定。这意味着,在屏幕上打印出“C++11”之前,std::cout 对象可能已经消失了。

英文原书都一个意思

std::cout’s lifetime is bound to the lifetime of the process. This means that the thread thr may be gone before std::cout prints C++11 onscreen.

在新版的 C++CoreGuidelines F.53 中,倒是没有了这段描述。

我想讨论目前描述的合理性,用户使用 detach 应该必然要求且保证 detach 的线程在主线程、进程执行完毕之前,就会结束。如果没有,那也不单单是 std::cout 会出现问题了。

比如b乎聊过的。

Mq-b avatar Dec 06 '23 12:12 Mq-b