Previously flag was set after pinging the condition variable, so waiting thread could woke up before the flag was set, and then the thread entered sleep again since condition was not met (yet). The problem was, nothing ever woke this thread as the "cs" was already spent. On some platforms the threads could actually wake up due to https://en.wikipedia.org/wiki/Spurious_wakeup but that's not guaranteed to ever happen.
Previously flag was set after pinging the condition variable, so waiting thread could woke up before the flag was set, and then the thread entered sleep again since condition was not met (yet). The problem was, nothing ever woke this thread as the "cs" was already spent. On some platforms the threads could actually wake up due to https://en.wikipedia.org/wiki/Spurious_wakeup but that's not guaranteed to ever happen.