pthread_mutex_lock(thread->state_lock);
while (thread->state == state)
pthread_cond_wait(thread->state_cond, thread->state_lock);
pthread_mutex_unlock(thread->state_lock);
pthread_mutex_lock(thread->state_lock);
while (thread->state == state)
pthread_cond_wait(thread->state_cond, thread->state_lock);
pthread_mutex_unlock(thread->state_lock);