user541686
user541686

Reputation: 210593

How do you use std::jthread::get_stop_token()?

I'm confused how std::jthread::get_stop_token is designed to work, because it seems to have an inherent race condition.

Namely, the executing thread can't simply call std::jthread on itself (as in this example) because it has no guarantee that the std::jthread object is actually constructed by the time that it begins executing. It seem to me that for a thread to use its own get_stop_token, it requires (at the very least) an extra event (like std::latch) solely to synchronize against its own construction.

However, I do not see any examples or mentions of this issue online, and so it seems to me that this may not be the intended usage. It does seem rather clunky and error-prone, as well as potentially inefficient as it requires the worker thread to block on the main thread before proceeding.

So how is get_stop_token supposed to be used?
Is there a simple example of the proper, intended usage of std::jthread::get_stop_token()?

Upvotes: 6

Views: 1958

Answers (2)

Liviu Stancu
Liviu Stancu

Reputation: 45

The problem in the example and in the accepted answer from that question is that the line j1 = std::jthread(&JT::init, this, std::stop_token()); involves a constructor and move operator, the second one concurring with auto st = j1.get_stop_token();. This can be fixed by using the constructor initializer list, which will not call the additional move operator:

JT() : j1(std::jthread(&JT::init, this)) {
}

Thanks to igor-tandetnik for clarifying that jthread object is guaranteed to be fully constructed by the time the thread function begins executing and suggesting the answer at my related question.

Upvotes: 0

bartop
bartop

Reputation: 10315

It appears from the example from here that get_stop_token is actually not meant to be used by the client code. It is called behind the scenes by the std::jthread and passed to the function called by std::jthread. It appears it is the way it's got to be done

#include <thread>
#include <iostream>
 
void f(std::stop_token stop_token, int value)
{
    while (!stop_token.stop_requested()) {
    }
}
 
int main()
{
    std::jthread thread(f, 5); 
}

Upvotes: 3

Related Questions