Use a condition variable to coordinate inter-thread-locking.

While here, correct the way of getting the error message in case
pthread_create(3) fails (it's wrong to use errno here, the return
value from pthread_create(3) should be used instead).