Thread synchronization with a condition