thread safety

use unique_lock for thread safety
create macros that ensure that the lock is released