> To kill the thread, set the stop flag and cond_signal the condvar
This is a race condition. When you "spin" on a condition variable, the stop flag you check must be guarded by the same mutex you give to cond_wait.
See this article for a thorough explanation: