Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleep operation in C++ on OS X

Tags:

c++

sleep

macos

I want to perform the above mentioned operation in milliseconds as the unit. Which library and function call should I prefer?

like image 695
m_j Avatar asked Oct 18 '13 00:10

m_j


People also ask

Is there a sleep command in C?

sleep() Function in C. sleep() function in C allows the users to wait for a current thread for a specific time. Other operations of the CPU will function properly but the sleep() function will sleep the present executable for the specified time by the thread.

How does sleep () work in C?

The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.

How do you put a Mac to sleep shortcut?

Option–Command–Power button* or Option–Command–Media Eject : Put your Mac to sleep. Control–Shift–Power button* or Control–Shift–Media Eject : Put your displays to sleep.


1 Answers

EDIT 2017: C++11 sleep_for is the right way to do this. Please see Xornad's answer, below.


C++03:

Since Mac OS X is Unix-based, you can almost always just use the standard linux functions!

In this case you can use usleep (which takes a time in microseconds) and just multiply your milliseconds by 1000 to get microseconds.

#include <unistd.h>
int main () {
    usleep(1000); // will sleep for 1 ms
    usleep(1); // will sleep for 0.001 ms
    usleep(1000000); // will sleep for 1 s
}

For more info on this function, check out the Linux man page:

http://linux.die.net/man/3/usleep

like image 103
NHDaly Avatar answered Oct 16 '22 15:10

NHDaly