rspec/rules/S6168/cfamily/rule.adoc
Fred Tingaud 22b4470f2a
Modify CFamily rules: CPP-4080 Refresh and standardize CppCoreGuidelines references (#3514)
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>
2023-12-20 10:08:18 +01:00

44 lines
1.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== 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 cant 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`]