Update the locking code to use RAII.
- move from directly calling try_lock/unlock on the mutex to using a unique_lock to ensure unlock is always called in the presence of exceptions.
- created a simple wrapper function to simplify getting the lock acquired right all the time.