I'm not completely sure if those functions aren't thread safe but in my experience I recommend you using the Thread sleep functions which are defined in the header thread. In the last multithreading application I developed, in some executions I had some random errors. They completely stopped when I started using the specific functions for threads. These functions are:
- std::this_thread::sleep_for: which makes the thread sleep for at least (due to thread scheduling it could be longer) the time given. Ex:
this_thread::sleep_for(chrono::milliseconds(sleepTime));
void sleep_for(const std::chrono::duration<Rep, Period>& sleep_duration);
- std::this_thread::sleep_until: which makes the thread sleep until the time point is reached (due to thread scheduling it could be longer too). Ex:
this_thread::sleep_until(chrono::system_clock::time_point timePoint);
void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time);
If I'm not mistaken the only real difference between both is the definition of the sleep time. And I think you can only use integers for sleepTime, I remember having compilation errors when I tried to use a float and chrono::seconds.