A new Thread with C++20: std::jthread

Contents[Show]

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

 timeline

 

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.

// threadJoinable.cpp

#include <iostream>
#include <thread>

int main(){
    
    std::cout << std::endl;
    std::cout << std::boolalpha;
    
    std::thread thr{[]{ std::cout << "Joinable std::thread" << std::endl; }};
    
    std::cout << "thr.joinable(): " << thr.joinable() << std::endl;
    
    std::cout << std::endl;
    
}

 

When executed, the program terminates.

threadJoinable

Both threads terminate. In the second run, the thread thr has enough time to display its message: "Joinable std::thread".

In the next example, I replace the header <thread> with "jthread.hpp" and use std::jthread from the upcoming C++ standard.

// jthreadJoinable.cpp

#include <iostream>
#include "jthread.hpp"

int main(){
    
    std::cout << std::endl;
    std::cout << std::boolalpha;
    
    std::jthread thr{[]{ std::cout << "Joinable std::thread" << std::endl; }};
    
    std::cout << "thr.joinable(): " << thr.joinable() << std::endl;
    
    std::cout << std::endl;
    
}

Now, the thread thr automatically joins in its destructor such as in this case if still joinable.

jthreadJoinable

Interrupt a std::jthread

To get a general idea, let me present a simple example.

// interruptJthread.cpp

#include "jthread.hpp"
#include <chrono>
#include <iostream>

using namespace::std::literals;

int main(){
    
    std::cout << std::endl;
    
    std::jthread nonInterruptable([]{                                   // (1)
        int counter{0};
        while (counter < 10){
            std::this_thread::sleep_for(0.2s);
            std::cerr << "nonInterruptable: " << counter << std::endl; 
            ++counter;
        }
    });
    
    std::jthread interruptable([](std::interrupt_token itoken){         // (2)
        int counter{0};
        while (counter < 10){
            std::this_thread::sleep_for(0.2s);
            if (itoken.is_interrupted()) return;                        // (3)
            std::cerr << "interruptable: " << counter << std::endl; 
            ++counter;
        }
    });
    
    std::this_thread::sleep_for(1s);
    
    std::cerr << std::endl;
    std::cerr << "Main thread interrupts both jthreads" << std:: endl;
    nonInterruptable.interrupt();
    interruptable.interrupt();                                          // (4)
    
    std::cout << std::endl;
    
}

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.

interruptJthread

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.

 interrupt token fixed

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.

 

std::jthread jthr([](std::interrupt_token itoken){
    ...
    std::interrupt_token interruptDisabled; 
    std::swap(itoken, interruptDisabled);     // (1)       
    ...
    std::swap(itoken, interruptDisabled);     // (2)
    ...
}

 

 std::interrupt_token interruptDisabled is not valid. This means that the thread can not accept an interrupt from the line (1) to (2) but after line (2) its possible.

Joining Threads

A std::jhread is a std::thread with the additional functionality to signal an interrupt and to automatically join(). To support this functionality it has a std::interrupt_token.

jthread

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.

template <class Predicate>
bool wait_until(unique_lock<mutex>& lock, 
                Predicate pred, 
                interrupt_token itoken);

template <class Rep, class Period, class Predicate>
bool wait_for(unique_lock<mutex>& lock, 
              const chrono::duration<Rep, Period>& rel_time, 
              Predicate pred, 
              interrupt_token itoken);

template <class Clock, class Duration, class Predicate>
bool wait_until(unique_lock<mutex>& lock, 
                const chrono::time_point<Clock, Duration>& abs_time, 
                Predicate pred, 
                interrupt_token itoken);

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.

cv.wait_until(lock, predicate, itoken);
if (itoken.is_interrupted()){
    // interrupt occurred
}

What's next?

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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, and Peter Ware.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, and Sudhakar Belagurusamy. 

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

Comments   

+1 #1 Vladimir 2019-04-20 18:27
Thanks for the summary. As a quick note: I believe that in the table summarizing the methods of itoken (in section "Interrupt Tokens"), the first method listed was supposed to be itoken.valid(), not itoken.value(). No?
Quote
0 #2 Rainer Grimm 2019-06-04 05:36
Quoting Vladimir:
Thanks for the summary. As a quick note: I believe that in the table summarizing the methods of itoken (in section "Interrupt Tokens"), the first method listed was supposed to be itoken.valid(), not itoken.value(). No?

Thanks, I fixed it but in the meantime the names changed once more.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 675

Yesterday 6382

Week 675

Month 167509

All 5464613

Currently are 127 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments