https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60966

--- Comment #16 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Hideaki Kimura from comment #14)
> void run_task(DummyTask* task) {
>     std::this_thread::sleep_for(std::chrono::milliseconds(100));
>     task->pr.set_value();
> }
> 
> void wait_for_task(DummyTask* task) {
>     task->pr.get_future().wait();

There's a race condition here between the calls to get_future() and
set_value(), so the program has undefined behaviour.

promise::get_future() is a non-const function that modifies the promise object,
therefore it must not be called while any other object is accessing the
promise.

The easiest fix is to call get_future() before passing the task into a new
thread, and store it in a std::vector<std::future<void>>

If I do that I can't make it hang any longer.

I'll keep looking into it though ...

Reply via email to