std::jthread stands for joining thread. In addition to
std::jthread automatically joins in its destructor and can cooperatively be interrupted. Read this post to know why
std::jthread should be your first choice.
The following table gives you a concise overview of the functionality of
First, why do we need an improved thread in C++20? Here is the first reason.
This is the non-intuitive behavior of
std::thread. If a
std::thread is still joinable, std::terminate is called in its destructor. A thread
thr is joinable if neither
thr.detach() was called. Let me show what that means.
When executed, the program terminates when the local object
thr goes out of scope.
Both executions of
std::thread terminate. In the second run, the thread
thr has enough time to display its message:
In the next example, I use
std::jthread from the C++20 standard.
Now, the thread
thr automatically joins in its destructor if it’s still joinable such as in this case.
But this is not all that
std::jthread provides additionally to
std::jthread can be cooperatively interrupted. I already presented the general ideas of cooperative interruption in my last post: Cooperative Interruption of a Thread in C++20.
Cooperative Interruption of a
To get a general idea, let me present a simple example.
In the main program, I start the two threads
nonInterruptable and interruptable (lines 1)and 2). Unlike in the thread
nonInterruptable , the thread
interruptable gets a
std::stop_token and uses it in line (3) to check if it was interrupted:
stoken.stop_requested(). In case of a stop request, the lambda function returns, and the thread ends. The call
interruptable.request_stop() (line 4) triggers the stop request. This does not hold for the previous call
nonInterruptable.request_stop() . The call has no effect.
To complete my post, with C++20, you can also cooperatively interrupt a condition variable.
New wait Overloads for
Before I write about
std::condition_variable_any, here are my post about condition variables.
The three wait variations
wait_until of the std::condition_variable_any get new overloads. These overloads take a
These new overloads need a predicate. The presented versions ensure getting notified if a stop request for the passed
std::stop_token stoken is signaled. They return a boolean that indicates whether the predicate evaluates to
true. This returned boolean is independent of whether a stop was requested or whether the timeout was triggered.
After the wait calls, you can check if a stop request occurred.
The following example shows the usage of a condition variable with a stop request.
The receiver thread (line 1) is waiting for the notification of the sender thread (line 2). Before the sender thread sends its notification (line 3), the main thread triggers a stop request in line (4). The program’s output shows that the stop request happened before the notification.
What happens when your write without synchronization to
std::cout? You get a mess. Thanks to C++20, we have synchronized output streams.