One of the participants in my CppCon 2018 workshop asked me: “Can a std::thread be interrupted?”. No, that was my answer, but this is not correct anymore. With C++20, we might get a std::jthread.
Let me continue my story from the CppCon 2018. During a break from my concurrency workshop, I had a chat with Nicolai (Josuttis). He asked me what I think about the new proposal P0660: Cooperatively Interruptible Joining Thread. At this point, I didn’t know the proposal. Nicolai is together with Herb Sutter and Anthony Williams one of the authors of the proposal. Today’s post is about the concurrent future. Here is the big picture to concurrency in current and upcoming C++.
From the title of the paper Cooperatively Interruptible Joining Thread you may guess that the new thread has two new capabilities: interruptable and automatic joining. Let me first write about automatically joining.
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 either thr.join() or thr.detach() was called.
When executed, the program terminates.
Both threads terminate. In the second run, the thread thr has enough time to display its message: “Joinable std::thread”.
In the following example, I replace the header <thread> with “jthread.hpp” and use std::jthread from the upcoming C++ standard.
Now, the thread thr automatically joins in its destructor, such as in this case if still joinable.
Interrupt a std::jthread
To get a general idea, let me present a simple example.
I started in the main program the two threads nonInterruptable and interruptable (lines 1 and 2). In contrast to the thread nonInterruptable, the thread interruptable gets a std::interrupt_token and uses it in line 3 to check if it was interrupted: itoken.is_interrupted(). In case of an interrupt, the lambda function returns, and, therefore, the thread ends. The call interruptable.interrupt() (line 4) triggers the end of the thread. This does not hold for the previous call nonInterruptable.interrupt(), which has no effect.
Here are more details to interrupt tokens, the joining threads, and condition variables.
An interrupt token std::interrupt_token models shared ownership and can be used to signal once if the token is valid. It provides three methods valid, is_interrupted, and interrupt.
If the interrupt token should be temporarily disabled, you can replace it with a default constructed token. A default constructed token is not valid. The following code snippet shows how to disable and enable a thread’s capability to accept signals.
std::interrupt_token interruptDisabled is not valid. This means that the thread can not accept an interrupt from line (1) to (2), but after line (2), it’s possible.
A std::jhread is a std::thread with the additional functionality to automatically signal an interrupt and join (). To support this functionality, it has a std::interrupt_token.
New Wait Overloads for Condition Variables
The two wait variations wait_for, and wait_until of the std::condition_variable get new overloads. They take a std::interrupt_token.
These new overloads require a predicate. The versions ensure to get notified if an interrupt is signalled for the passed std::interrupt_token itoken. After the wait calls, you can check if an interrupt occurred.
As I promised in my last post, my next post is about the remaining rules for defining concepts.
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,