One of the participants in my CppCon 2018 workshop asked me: "Can a std::thread be interrupted?". No, 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 of 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 automatically joining. Let me first write about automatically joining.

Automatically joining

This is the non-intuitive behaviour 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() nor thr.detach() was called.

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 does not have an effect.

Here are more details to interrupt tokens, the joining threads, and condition variables.

Interrupt Tokens

An interrupt token std::interrupt_token models shared ownership and can be used to signal once if the token is valid. It provides the 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 the capability of a thread to accept signals.

This 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.

Get your e-book at Leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11,C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.

Get your interactive course at educative

Modern C++ Concurrency in Practice: Get the most out of any machine

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.