You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+5-28Lines changed: 5 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -14516,7 +14516,7 @@ Flag attempts to pass local variables to a thread that might `detach()`.
14516
14516
14517
14517
A `joining_thread` is a thread that joins at the end of its scope.
14518
14518
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).
14520
14520
14521
14521
##### Example, bad
14522
14522
@@ -14549,45 +14549,22 @@ It is harder to ensure absence of errors in detached threads (and potentially de
14549
14549
t2.join();
14550
14550
} // one bad bug left
14551
14551
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
-
14575
14552
##### Note
14576
14553
14577
14554
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).
14579
14556
14580
14557
##### Note
14581
14558
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.
14583
14560
14584
14561
##### Enforcement
14585
14562
14586
14563
Flag uses of `std::thread`:
14587
14564
14588
-
* Suggest use of `gsl::joining_thread`.
14565
+
* Suggest use of `gsl::joining_thread` or C++20 `std::jthread`.
14589
14566
* 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.
14591
14568
14592
14569
### <a name="Rconc-detached_thread"></a>CP.26: Don't `detach()` a thread
0 commit comments