mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
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:
parent
45ad5c4628
commit
c379d3f6ea
|
@ -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.
|
A `joining_thread` is a thread that joins at the end of its scope.
|
||||||
Detached threads are hard to monitor.
|
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
|
##### Example, bad
|
||||||
|
|
||||||
|
@ -14549,45 +14549,22 @@ It is harder to ensure absence of errors in detached threads (and potentially de
|
||||||
t2.join();
|
t2.join();
|
||||||
} // one bad bug left
|
} // 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
|
##### Note
|
||||||
|
|
||||||
Make "immortal threads" globals, put them in an enclosing scope, or put them on the free store rather than `detach()`.
|
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
|
##### 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
|
##### Enforcement
|
||||||
|
|
||||||
Flag uses of `std::thread`:
|
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.
|
* 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
|
### <a name="Rconc-detached_thread"></a>CP.26: Don't `detach()` a thread
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user