implementation of pthread_cond_wait()