c++ condition variable notification not working as expected c++ condition variable notification not working as expected multithreading multithreading

c++ condition variable notification not working as expected


std::async returns a std::future holding result of function execution. In your case, it is a temporary object which is immideatly destroyed. The documentation for std::future says:

these actions will not block for the shared state to become ready, except that it may block if all of the following are true:

✔ the shared state was created by a call to std::async

✔ the shared state is not yet ready

✔ this was the last reference to the shared state

All of those are true, so destruction of that future will block until worker function will finish executing.

You can create detached thread to avoid this problem:

std::thread(worker_thread).detach();