From c379d3f6eaba5e50dbb640a41f3e086da1c4572c Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Thu, 2 Jul 2020 14:07:46 -0400 Subject: [PATCH] Remove a broken example, and copyedit. (#1637) Using `this` in a free function makes the program ill-formed; it doesn't magically give you a `std::thread*`. --- CppCoreGuidelines.md | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 3f0d5a4..f8f3f0b 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -14516,7 +14516,7 @@ Flag attempts to pass local variables to a thread that might `detach()`. 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) +It is harder to ensure absence of errors in detached threads (and potentially detached threads). ##### Example, bad @@ -14549,45 +14549,22 @@ It is harder to ensure absence of errors in detached threads (and potentially de t2.join(); } // one bad bug left - -##### Example, bad - -The code determining whether to `join()` or `detach()` may be complicated and even decided in the thread of functions called from it or functions called by the function that creates a thread: - - void tricky(thread* t, int n) - { - // ... - if (is_odd(n)) - t->detach(); - // ... - } - - void use(int n) - { - thread t { tricky, this, n }; - // ... - // ... should I join here? ... - } - -This seriously complicates lifetime analysis, and in not too unlikely cases makes lifetime analysis impossible. -This implies that we cannot safely refer to local objects in `use()` from the thread or refer to local objects in the thread from `use()`. - ##### Note Make "immortal threads" globals, put them in an enclosing scope, or put them on the free store rather than `detach()`. -[don't `detach`](#Rconc-detached_thread). +[Don't `detach`](#Rconc-detached_thread). ##### Note -Because of old code and third party libraries using `std::thread` this rule can be hard to introduce. +Because of old code and third party libraries using `std::thread`, this rule can be hard to introduce. ##### Enforcement Flag uses of `std::thread`: -* Suggest use of `gsl::joining_thread`. +* Suggest use of `gsl::joining_thread` or C++20 `std::jthread`. * Suggest ["exporting ownership"](#Rconc-detached_thread) to an enclosing scope if it detaches. -* Seriously warn if it is not obvious whether if joins of detaches. +* Warn if it is not obvious whether a thread joins or detaches. ### CP.26: Don't `detach()` a thread