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

Viewed 548

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()?

1 Answers

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); 
}
Related