A typical question in my C++ seminars is: Can a thread be killed? Before C++20, my answer is no. With C++20, you can ask a thread politely for its interruption.
First of all. Why is it not a good idea to kill a thread? The answer is relatively easy. You don’t know in which state the thread is when you kill it. Here are two possible malicious outcomes.
- The thread is only half-done with its job. Consequently, you don’t know the state of that job and, hence, the state of your program. You end with undefined behavior, and all bets are open.
- The thread may be in a critical section and locks a mutex. Killing a thread while it locks a mutex ends with a high probability in a deadlock.
Okay, killing a thread is not a good idea. You may ask a thread friendly if it is willing to stop. This is precisely what cooperative interruption in C++20 means. You ask the thread, and the thread can accept or ignore your wish for the interruption.
The additional functionality of the cooperative interruption thread in C++20 is based on the
std::stop_callback, and the
std::stop_source data types.
std::stop_callback, or a
std::stop_source allows a thread to asynchronously request an execution to stop or ask if an execution got a stop signal. The
std::stop_token can be passed to an operation and afterward be used to poll the token for a stop request actively or to register a callback via
std::stop_callback. The stop request is sent by a
std::stop_source. This signal affects all associated
std::stop_token. The three classes
std::stop_callback share the ownership of an associated stop state. The calls
stop_possible() are atomic.
You can construct a
std::stop_source in two ways:
The default constructor (1) constructs a
std::stop_source with a new stop state. The constructor taking
std::nostopstate_t (2) constructs an empty
std::stop_source without associated stop state.
std::stop_source src provides the following member functions for handling stop requests.
src.stop_possible() means that
src has an associated stop state.
src has an associated stop state and was not asked to stop earlier.
src.request_stop() is successful and returns
src has an associated stop state, and it was not requested to stop before.
src.get_token() returns the stop token
stoken. Thanks to
stoken you can check if a stop request has been made or can be made for its associated stop source
src. The stop token
stoken observes the stop source
The following table presents the member functions of a
A default-constructed token that has no associated stop state.
stoken.stop_possible also returns
stoken has an associated stop state.
true when the stop token has an associated stop state and has already received a stop request.
std::stop_token should be temporarily disabled, you can replace it with a default constructed token. A default constructed token has no associated stop-state. The following code snippet shows how to disable and enable a thread’s capability to accept stop requests.
std::stop_token interruptDisabled has no associated stop state. This means the thread
jthr can, in all lines except lines (1) and (2), accept stop requests.
When you study the code snippet carefully, you may wonder if the used
std::jthread. std::jthread in C++20 is an extended
std::thread in C++11. The j in
jthread stands for joinable because it joins automatically in its destructor. Its first name was
ithread. You may guess why: i stands for interruptible. I will present
std::jthread in my next post.
My next example shows the use of callbacks using a
Each of the ten threads invokes the lambda function
func (1). The callback (2) displays the thread
id and the
counter. Due to the one-second sleeping of the main thread (3) and the sleeping of the child threads, the counter is 4 when the callbacks are invoked. The call
thr.request_stop() triggers the callback on each thread.
std::thread has one big weakness. When you forget to join it, its destructor calls
std::terminate, and your program crashed.
std::jthread (C++20) overcomes this counter-intuitive weakness and is also interruptible.
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,