I have thread 1 executing the following code:
while(condition == true)
condition = false;
T1: condition checks true
T2: condition set to false
You should fix this race condition by having thread 2 lock the condition's mutex before changing the flag.
You are describing a typical race condition that happens for unprotected flags and conditions. These race conditions are the reason for the mutex lock pattern in condition usage. Put simply, always have a mutex protect the variables involved in checking a condition value.
In code for thread 2:
unique_lock<mutex> ul(m); condition = false; cv.notify_one();