What does the C function โ€œSleepโ€ (capital โ€œSโ€) on a Mac do?

Pay attention to the capital "S" in Sleep . Sleep with capital "S" is a standard feature that falls asleep in milliseconds on a PC. Mac OS X does not have this symbol. However, the Xcode communications framework seems to find something to which it is attached. What is it?

+4
sleep xcode macos
source share
6 answers

Well, this is the old old Carbon feature (under CoreServices / OSServices) that makes the computer sleep. I can not find the documentation.

+6
source share

sleep (int) is a method from a unix system that runs mac. Known as Darwin.

Here is the manpage for sleep

Essentially, this is a C call that allows you to tell the computer about sleep mode for "int" the number of seconds.

Alternatively, you can use "usleep (unsigned int)", which will sleep for "unsigned int" the number of "microseconds", which is the second * 1000 * 1000 or 1000 milliseconds.

Here is ManPage for firmware

Both of these functions are wrapped to access the basic "C / C ++" methods that a typical C / C ++ developer will use.

here is an example of equivalent code

 NSTimeInterval sleepTime = 2.0; //Time interval is a double containing fractions of seconds [NSThread sleepForTimeInterval:sleepTime]; // This will sleep for 2 seconds sleep((int)sleepTime); // This will also sleep for 2 seconds 

if you want more granularity, you will need usleep (unsigned int), which will give you a much more accurate number.

 NSTimeInterval sleepTime = 0.2; // This is essentially 2 tenths of a second [NSThread sleepForTimeInterval:sleepTime]; // This will sleep for 2 tenths of a second; usleep((unsigned int)(sleepTime * 1000 * 1000)); // This will also sleep for 2 tenths of a second 

I hope this helps

+2
source share

The equivalent of sleep should be

[ NSThread sleepForTimeInterval:5.0];

However, this is in seconds. To use milliseconds, I think you need to use usleep (num * 1000), where num is the number of mills

But I do not know what sleep (...) does

0
source share

On mac, under OSX, there is no such symbol.

I do not think that there is such a symbol in the classic mac. I even looked into my ancient copy of THINK Reference .

I would also be surprised to find the Sleep function (with capital S) in C, as many people call C functions using all lowercase letters.

Was you asked to ask a question because you are getting an error message?

0
source share

There is a usleep () command.

 pthread_create(&pth,NULL,threadFunc,"foo"); while(i < 100) { usleep(1); printf("main is running...\n"); ++i; } printf("main waiting for thread to terminate...\n"); pthread_join(pth,NULL); return 0; 
0
source share

Do you use any other libraries in your project?

I get compilation errors using Cocoa and Carbon projects using Apple template projects, however, I notice that sleep functions (using the definition) are functions in both cross-platform SDL module libraries and SFML, and possibly for many others.

Have you tried your sample code in a template project using only apple libraries?

It is possible that Sleep() is the function you are referring to.

0
source share

All Articles