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*`.
This commit is contained in:
Arthur O'Dwyer 2020-07-02 14:07:46 -04:00 committed by GitHub
parent 45ad5c4628
commit c379d3f6ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.
### <a name="Rconc-detached_thread"></a>CP.26: Don't `detach()` a thread