未验证 提交 c379d3f6 编写于 作者: A Arthur O'Dwyer 提交者: GitHub

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*`.
上级 45ad5c46
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册