Working on C++ multithreading with race condition protection