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.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,