CMake changes

+ use RAII objects for synchronization