Reputation: 2642
Are there any parts of std::coroutine_handle
that are defined as thread safe in the standard?
I could for example see std::coroutine_handle::done()
being implemented with an atomic variable, which would allow for completion checks without locking everything first.
But if nothing related to thread safety is defined in the standard then I would have to assume the worst case scenario and always lock everything.
Upvotes: 2
Views: 516
Reputation: 473966
None of the functions of coroutine_handle
are specified to not provoke data races. Therefore, the standard library's common rules apply: concurrently calling any functions with an object provokes a data race on that object unless all potentially conflicting functions access the object via a const
pointer/reference (like const
members).
The observers, such as explicit operator bool()
and done
are both const
and thus do not provoke a data race, unless other, non-const
functions are being called. And of course, operator()
and resume()
are both non-const
, and thus can provoke data races with the observers. However, the observers have the precondition that the handle in question is suspended, so you couldn't really do that anyway.
Really though, you shouldn't be trying to access a handle concurrently to begin with. The promise type should manage the handle for these scenarios, and any interaction between the future and the handle should happen through the promise. And if concurrent interaction is needed, the promise can provide it.
Upvotes: 7