Sleep() vs sleep_for()

12,535

Solution 1

The difference is that sleep_for() is defined by the C++11 standard, and Sleep() is defined by the Windows API. If you use sleep_for(), it is quite likely, though not certain, that the compiler will generate code that calls Sleep() when compiling for Windows. However, since it is a C++11 standard function, that means that any compiler (correctly) implementing the C++11 standard will have some way to generate code for the functionality described by the function for any platforms it supports.

The other major difference is that sleep_for() takes an std::chrono::duration as a parameter instead of an integer in milliseconds. This allows you to more easily and more precisely specify the time for which you want the thread to sleep. It also moves some documentation information into the type system.

You wanted to know the implications of sleep_for() versus Sleep() for multithreading, and all I can say is that sleep_for() has the implications defined in the C++11 standard, and Sleep(), has the implications defined in the Windows API. And if you check the reference, each one talks about its respective thread type. So if you're using C++11 threads, use sleep_for(). If you're using Win32 threads directly, use Sleep(). Sleep() does not have any notion of C++11 threads, and so does not have clearly defined behavior. Likewise, sleep_for() does not have a notion of Windows API threads, and so does not have clearly defined behavior there either. The documentation for each function specifies its interactions with its respective threads. Don't mix standards.

Solution 2

While you call a sleep method like below it instructs the O/S to suspend the thread for that specific time slice provided as parameter to sleep() function/method. Once this function execute O/S temporarily stop that thread and put on sleep mood. By doing that O/S also release the micro processor from it(from the sleeping thread) so that processor can be assign to another thread or process which is waiting for a processor time. There is no any major difference between the two. Sleep() is for windows and you have to add windows header file <windows> and next one, you can use in both windows and Unix environment and need to use header file <chrono>.

 Sleep(); //from Win32
 std::this_thread::sleep_for();

You can use both the sleep function like below:-

 Sleep(100); //sleep for 100 m/s but only in windows    
 std::this_thread::sleep_for(std::chrono::milliseconds(100));//sleep for 100 m/s. Can be use in both windows and Unix environment

After 100 m/s O/S will wake up the sleeping thread and reassign the processor to it for further execution and operation.

Share:
12,535

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    Can someone explain the difference in behavior between these two ways of stopping a thread and then continue it again?.

    Sleep(); //from Win32
    
    std::this_thread::sleep_for();
    

    I remark in terms of multithreading behavior not in systems compatibility.

    • llllllllll
      llllllllll about 6 years
      You mean sleep() in unistd.h?
    • Johnny Mopp
      Johnny Mopp about 6 years
      Or Win32 Sleep()?
    • 0x5453
      0x5453 about 6 years
      Sleep has this caveat: A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution. Also Sleep only takes millis whereas sleep_for can take any duration.
    • Andrei Damian
      Andrei Damian about 6 years
      Sleep is a windows api function. sleep_for is a c++ standard function. sleep_for, on windows, is probably implemented with Sleep or another equivalent.
    • user1703401
      user1703401 about 6 years
      Pretty safe to assume that sleep_for() calls the OS function. So no difference. Sleeping a thread is gauche.
    • NathanOliver
      NathanOliver about 6 years
      There shouldn't be any difference in how they behave. Portability though is a different aspect. std::this_thread::sleep_for() should compile on any C++11 compiler. Sleep() will only compile on windows.
  • user3224083
    user3224083 over 3 years
    Slightly off topic, but a side benefit of using std::this_thread::sleep_for() is the inherent readability of the argument. e.g sleep_for(std::chrono::hours(1)) vs sleep(3600).