Should I exchange sleep_for hibernation

I'm in the process of updating some legacy C ++ code to C ++ 11 under Linux using gcc. When trying to set priorities, I asked the following question. Could there be any advantage in exchanging a call for usleep with a call to std::this_thread::sleep_for ? In the code, I say that a working thread must wait a very short period. Therefore, I do not need any additional features, such as sleep interruption.

+5
source share
1 answer

Yes. std::this_thread::sleep_for is defined by the C ++ 11 standard and, therefore, is a portable solution on any system with a C ++ 11 compiler and a standard library.

usleep is specified by POSIX.1-2001 (and is deprecated!), which means that it can (reliably) be used only on POSIX-compatible systems.

POSIX.1-2008 removes the usleep specification, in favor of nanosleep . For this reason, std::this_thread::sleep_for is a much better choice.

(For more details see http://linux.die.net/man/3/usleep .

+9
source

Source: https://habr.com/ru/post/1232710/


All Articles