What does C “Sleep” function (capital “S”) do on a Mac?

三世轮回 提交于 2019-12-22 08:13:12

问题


Note the capital "S" in Sleep. Sleep with a capital "S" is a standard function that sleeps milliseconds on the PC. On Mac OS X, there is no such symbol. However, the Xcode linking environment seems to find something to link it to. What is it?


回答1:


Well, it’s an old old Carbon function (in the CoreServices / OSServices framework) that puts the computer to sleep. I can’t find any documentation.

  • Sleep and Xcode



回答2:


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

Here is the ManPage for sleep

Essentially it is a C call that lets you tell the computer to sleep for 'int' number of seconds.

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

Here is the ManPage for usleep

Both of these functions are wrapped to allow you access to the underlying "C/C++" methods that a normal C/C++ developer would use.

here is an equivalent code example

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 wish to have more granularity you will need usleep(unsigned int) which will give you a much more precise 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 that helps




回答3:


The equivalent to sleep should be

[NSThread sleepForTimeInterval:5.0];

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

But I don't know what Sleep(...) does




回答4:


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

I don't think there is such a symbol in Classic mac either - I even looked in my ancient copy of THINK Reference.

I would also be surprised to find a Sleep (with a capital S) function in C, since many people name C functions using all lower case.

Were you prompted to ask the question because you're getting a link error?




回答5:


There is usleep().

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;



回答6:


Are you using any other libraries in your project?

I'm getting compile errors using both Cocoa and Carbon using apple's template projects, however I notice that sleep functions (using the definition) are a feature in both the SDL and SFML cross platform libraries, and perhaps for many others.

Have you tried your code example on a template project using only with apples libraries?

It could be that Sleep() is a function in something else you are linking to.



来源:https://stackoverflow.com/questions/3827158/what-does-c-sleep-function-capital-s-do-on-a-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!