Skip to content

Commit c379d3f

Browse files
authored
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*`.
1 parent 45ad5c4 commit c379d3f

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

CppCoreGuidelines.md

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14516,7 +14516,7 @@ Flag attempts to pass local variables to a thread that might `detach()`.
1451614516

1451714517
A `joining_thread` is a thread that joins at the end of its scope.
1451814518
Detached threads are hard to monitor.
14519-
It is harder to ensure absence of errors in detached threads (and potentially detached threads)
14519+
It is harder to ensure absence of errors in detached threads (and potentially detached threads).
1452014520

1452114521
##### Example, bad
1452214522

@@ -14549,45 +14549,22 @@ It is harder to ensure absence of errors in detached threads (and potentially de
1454914549
t2.join();
1455014550
} // one bad bug left
1455114551

14552-
14553-
##### Example, bad
14554-
14555-
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:
14556-
14557-
void tricky(thread* t, int n)
14558-
{
14559-
// ...
14560-
if (is_odd(n))
14561-
t->detach();
14562-
// ...
14563-
}
14564-
14565-
void use(int n)
14566-
{
14567-
thread t { tricky, this, n };
14568-
// ...
14569-
// ... should I join here? ...
14570-
}
14571-
14572-
This seriously complicates lifetime analysis, and in not too unlikely cases makes lifetime analysis impossible.
14573-
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()`.
14574-
1457514552
##### Note
1457614553

1457714554
Make "immortal threads" globals, put them in an enclosing scope, or put them on the free store rather than `detach()`.
14578-
[don't `detach`](#Rconc-detached_thread).
14555+
[Don't `detach`](#Rconc-detached_thread).
1457914556

1458014557
##### Note
1458114558

14582-
Because of old code and third party libraries using `std::thread` this rule can be hard to introduce.
14559+
Because of old code and third party libraries using `std::thread`, this rule can be hard to introduce.
1458314560

1458414561
##### Enforcement
1458514562

1458614563
Flag uses of `std::thread`:
1458714564

14588-
* Suggest use of `gsl::joining_thread`.
14565+
* Suggest use of `gsl::joining_thread` or C++20 `std::jthread`.
1458914566
* Suggest ["exporting ownership"](#Rconc-detached_thread) to an enclosing scope if it detaches.
14590-
* Seriously warn if it is not obvious whether if joins of detaches.
14567+
* Warn if it is not obvious whether a thread joins or detaches.
1459114568

1459214569
### <a name="Rconc-detached_thread"></a>CP.26: Don't `detach()` a thread
1459314570

0 commit comments

Comments
 (0)