At least you are not correctly handling the usage of condition variable and mutex.
The mutex shall be locked by the thread calling pthread_cond_wait()
prior to calling the latter.
Verbatim from man pthread_cond_wait
:
pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled. The thread execution is suspended and does not consume any CPU time until the condition variable is signaled. The mutex must be locked by the calling thread on entrance to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait re-acquires mutex (as per pthread_lock_mutex).
Also the mutex is locked after returning form the call to pthread_cond_wait()
and must be unlocked then somewhere sometime.