What can be the alternate of this for macOS? I was making a vaccine management system and now this is the issue I am facing.
Asked
Active
Viewed 835 times
-4
-
3If sleep is all you need, googling would have been faster: [Sleep function in C++](https://stackoverflow.com/questions/1658386/sleep-function-in-c) – alter_igel Aug 16 '21 at 17:46
-
5[`windows.h`](https://en.wikipedia.org/wiki/Windows.h) is a Microsoft-specific header for Win32 that only works on windows. You may find suitable alternatives on mac based on what you are looking for. – Ruks Aug 16 '21 at 17:46
-
Depending on the project it could be easy to remove the windows.h dependency or very hard. If it is the latter wine may help. [https://www.winehq.org/](https://www.winehq.org/) possibly: [https://wiki.winehq.org/Winelib_User%27s_Guide](https://wiki.winehq.org/Winelib_User%27s_Guide) – drescherjm Aug 16 '21 at 17:47
1 Answers
3
Instead of using platform dependent function to put the thread in sleep, you should use function available in standard C++ library.
C++ provides std::this_thread::sleep_for()
function to put a thread in sleep.
Please check example here taken from CppReference page:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::chrono_literals;
std::cout << "Hello waiter\n" << std::flush; auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(2000ms);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end-start;
std::cout << "Waited " << elapsed.count() << " ms\n";
}

user4581301
- 33,082
- 7
- 33
- 54

cse
- 4,066
- 2
- 20
- 37
-
1[Looks like we might have to update that cppreference docs page.](https://stackoverflow.com/a/37440647/4581301) If `high_resolution_clock` is an alias of `system_clock` the timing result can be hilariously wrong. – user4581301 Aug 16 '21 at 17:56
-
`high_resolution_clock` is not reliable for measuring durations. It's pretty unreliable in general, despite its impressive-sounding name. `steady_clock` should be used here. – Drew Dormann Aug 16 '21 at 18:00