Why is a mutex lock required before calling pthread_cond_wait?
Because otherwise there is an inevitable race condition.
Mutex protects shared state. A condition variable is associated with some predicate ("condition") in the state. The basic idea is that you want:
1) check the predicate
2) if the predicate is false, go to sleep mode until it becomes true.
In a parallel system, for some flow it is always possible to make the predicate true between (1) and (2). To avoid this race, you must hold the mutex to (1), and you must atomize it while doing (2).
For example, for a queue, the predicate may be โqueue is not emptyโ. But meanwhile, as you check to see if the queue is in turn and the time when you go to sleep, some other thread may add something to the queue.
Thus, you must keep the mutex both when checking the predicate and when calling pthread_cond_wait.
In addition, is it necessary to make a lock (in the same mutex) before calling pthread_cond_signal?
As far as I know, there is no fundamental problem with this; it simply introduces potential inefficiency.
Here again, any shared state that you change (and thus the predicate true) must be protected by a mutez. Therefore, anytime you want to signal a status, you should still have a mutex.
If you release the mutex before the condition signal, the predicate may become false between them due to the action of some other thread. This race does not crash because any thread waiting for a state must in any case double-check the predicate before continuing ... But why did it cause a problem?
Bottom line: just follow the instructions and you donโt even need to think about these issues. :-)