7

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.

  • 1
    You mean `sleep()` in `unistd.h`? – llllllllll Mar 02 '18 at 14:40
  • 2
    Or Win32 [`Sleep()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396)? – 001 Mar 02 '18 at 14:41
  • 1
    [`Sleep`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx) 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. – 0x5453 Mar 02 '18 at 14:41
  • 4
    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. – Andrei Damian Mar 02 '18 at 14:42
  • I mean Win32 Sleep(). –  Mar 02 '18 at 14:43
  • My slightly flippant answer would be, the difference is: “error: 'Sleep' was not declared in this scope”, or, alternatively: “fatal error: windows.h: No such file or directory”. – Konrad Rudolph Mar 02 '18 at 14:43
  • Do you want someone to read [the documentation on `Sleep`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx) and [the documentation on `std::this_thread::sleep_for`](http://en.cppreference.com/w/cpp/thread/sleep_for) and put the differences into a nice list so you can save time reading? – nwp Mar 02 '18 at 14:44
  • 3
    Pretty safe to assume that sleep_for() calls the OS function. So no difference. Sleeping a thread is gauche. – Hans Passant Mar 02 '18 at 14:44
  • @nwp Yes, that would be a good answer. – Konrad Rudolph Mar 02 '18 at 14:44
  • In terms of multithreading behavior: `Sleep` behaves exactly as documented in the Microsoft documentation. The exact behaviour of `std::this_thread::sleep_for();` is implementation dependent, it _may_ just end up calling `Sleep` or it may do it otherways. Anyway it is conformant to [this documentation](http://en.cppreference.com/w/cpp/thread/sleep_for). Check the source code if you want to know for sure what is going on under the hood. – Jabberwocky Mar 02 '18 at 14:58
  • 1
    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. – NathanOliver Mar 02 '18 at 14:59

2 Answers2

14

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.

  • 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). – user3224083 Sep 23 '20 at 12:28
  • @user3224083 You can use [user-defined literals](https://en.cppreference.com/w/cpp/language/user_literal) in C++ to make them even more readable e.g. `this_thread::sleep_for(1h)` or `this_thread::sleep_for(500ms)` or `this_thread::sleep_for(10s)`. See https://en.cppreference.com/w/cpp/chrono/duration#Literals for more information – Debajit Jun 25 '23 at 04:26
0

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.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17