The basic problem here seems to be that this thread keeps the mutex locked by default, and only unlocks it for a limited period of time when it's sure it doesn't need to the mutex.
Normally, you want to do roughly the opposite: the mutex is unlocked by default, and only gets locked for a short period of time when absolutely necessary. Without knowing at least a little bit about the rest of the code, it's hard to be sure of the right way to do that though.
One possibility would be restructure the code so that only a single thread has to deal directly with the shared resources, and the other two threads each have a queue of requests they submit to that thread. That thread can then prioritize the requests, so both client threads get an equal chance to do their thing with the shared resources (or maybe not necessarily equal, but an appropriate amount for each).
As an aside, if you can't use C++11 threading classes, I'd at least consider writing rough analogs of them--a mutex
class that calls pthread_mutex_create
in its ctor, pthread_mutex_destroy
in its dtor, and has a lock
and unlock
that call pthread_mutex_lock
and pthread_mutex_unlock
respectively.
Likewise, a lock_guard
that's passed a mutex that it locks in its ctor, and unlocks in its dtor (and possibly a similar wrapper for condition variables, etc.) Bottom line: if you're starting from pthreads, you can write a pretty substantial subset of the standard library threading pretty quickly and easily.
Both of these are pretty trivial, and simplify the rest of the code enough that the time to develop them will be repaid quite quickly. In addition, they'll get you things like exception safety that are much more difficult to implement in most other ways.