Add pthread lock C++ wrapper, and convert code to use it.

This provides RAII semantics for pthread locking. In particular, it can be used
to ensure a lock is released when the scope is exited.
Change locking behavior to avoid a race condition.

It is possible that data could come in between when Select() checks to see if
any data is already available and when it blocks on the conditional variable.
If that happens, then the conditional signal may be sent before we block on it,
causing us to miss it. This means we would block until more data shows up, even
though there is data ready.

This is resolved by 1) getting the lock before instead of after checking if the
data is already there, and 2) getting the lock before sending the conditional
signal. This will prevent a signal from being sent during this critical period.
The lock is implicity released by waiting for the signal, so if there is a
thread blocked that is about to send a signal, it will unblock at that time.