You have a while loop, ending only if flag != 1.
Is flag set to semething else in the code after the wait function? In that case you must use Sleep ( ) to wait, like Nosferatu suggested.
But, if flag is set to semething else by a different thread, it might be better to use some of the synchronisation API calls, like a critical section or a mutex.
Marcel