Replaced bool flag variable with std::atomic bool