Cpp scoped lock

Using safer scoped lock in cpp wrapper (Resource Acquisition is Initialization idiom) and simplifying some methods using early exits which are simpler to do with scoped locks.