> std::unique_lock<std::mutex> lck(mtx); This statement will lock the mutex in constractor.
> I miss understand the behavier with std::lock_guard
Yes.
1. The constructor of std::unique_lock<std::mutex> acquires the lock.
2. When cv.wait(lck) is executed, it atomically releases lock and blocks the thread.
3. When the thread is unblocked (with cv.notify_all()) the lock is reacquired before cv.wait(lck); returns.
4. The while loop while (!ready) is there because the thread may be unblocked spuriously. cv.wait( lck, []{ return ready ;} ); is equivalent to while (!ready) cv.wait(lck)
Note that waiting for a std::condition_variable works only with std::unique_lock<std::mutex>
In the function go, modifying the variable ready while holding a less expensive std::lock_guard is adequate;
since the thread does not wait on a condition variable.