
Update all links to C++ Core Guidelines to `e49158a`.
Refresh done using the following script and some manual edits:
db76e34e74/personal/fred-tingaud/rspec/refresh-cppcoreguidelines.py
When re-using this script, be mindful that:
- it does not cover `shared_content`
- it does not properly escape inline code in links (e.g., "[=]" or "`mutex`es")
- it does not change `C++` to `{cpp}` in link titles.
Co-authored-by: Marco Borgeaud <marco.borgeaud@sonarsource.com>
44 lines
1.3 KiB
Plaintext
44 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
||
|
||
``++std::jthread++``, introduced in {cpp}20, is a wrapper around ``++std::thread++``. This way, it has the same functionalities as ``++std::thread++``, making the substitution straightforward while adding two interesting behaviors:
|
||
|
||
* It joins by default in its destructor. If a ``++std::thread++`` was not joined or detached before being destroyed, a call to ``++std::terminate++`` was made. This behavior can’t happen with ``++std::jthread++``.
|
||
* It can be canceled or stopped in some situations by calling ``++request_stop()++``.
|
||
|
||
This rule raises an issue when ``++std::thread++`` is used.
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
[source,cpp,diff-id=1,diff-type=noncompliant]
|
||
----
|
||
void backgroundTask();
|
||
int main() {
|
||
std::thread t(backgroundTask); // Noncompliant
|
||
t.join();
|
||
}
|
||
----
|
||
|
||
|
||
=== Compliant solution
|
||
|
||
[source,cpp,diff-id=1,diff-type=compliant]
|
||
----
|
||
void backgroundTask();
|
||
int main() {
|
||
std::jthread jt(backgroundTask);
|
||
}
|
||
----
|
||
|
||
|
||
== Resources
|
||
|
||
=== Documentation
|
||
|
||
* {cpp} reference - https://en.cppreference.com/w/cpp/thread/jthread[std::jthread]
|
||
|
||
=== External coding guidelines
|
||
|
||
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#cp25-prefer-gsljoining_thread-over-stdthread[CP.25: Prefer `gsl::joining_thread` over `std::thread`]
|
||
|