xcode C“睡眠”功能(大写“S”)在 Mac 上有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3827158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does C "Sleep" function (capital "S") do on a Mac?
提问by ddyer
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?
注意 中的大写“S” Sleep
。 Sleep
大写的“S”是一个标准函数,它在 PC 上休眠毫秒。在 Mac OS X 上,没有这样的符号。但是,Xcode 链接环境似乎找到了可以链接到的内容。它是什么?
回答by Josh Lee
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.
嗯,这是一个旧的旧 Carbon 函数(在 CoreServices / OSServices 框架中),它使计算机进入睡眠状态。我找不到任何文档。
回答by The Lazy Coder
sleep(int) is a method from the unix system that run mac Known as Darwin.
sleep(int) 是 unix 系统中运行 mac 的一种方法,称为 Darwin。
Essentially it is a C call that lets you tell the computer to sleep for 'int' number of seconds.
本质上,它是一个 C 调用,可让您告诉计算机休眠 'int' 秒数。
alternatively you can use 'usleep(unsigned int)' which will sleep for 'unsigned int' number of "microseconds" which is second * 1000 * 1000 or 1000 milliseconds.
或者,您可以使用 'usleep(unsigned int)' 它将休眠 'unsigned int' 数的“微秒”,即秒 * 1000 * 1000 或 1000 毫秒。
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.
这两个函数都被包装起来,以允许您访问普通 C/C++ 开发人员会使用的底层“C/C++”方法。
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.
如果你希望有更多的粒度,你将需要 usleep(unsigned int) 这会给你一个更精确的数字。
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
我希望有帮助
回答by Tomas
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.
我在使用 Apple 的模板项目时同时使用 Cocoa 和 Carbon 遇到编译错误,但是我注意到睡眠函数(使用定义)是 SDL 和 SFML 跨平台库中的一个功能,也许还有许多其他功能。
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.
这可能是Sleep()
您正在链接的其他功能中的一个功能。
回答by Preet Sangha
The equivalent to sleep should be
相当于 sleep 应该是
[NSThread sleepForTimeInterval:5.0];
[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
然而,这是在几秒钟内。要使用毫秒,我认为您必须使用 usleep( num * 1000),其中 num 是工厂数
But I don't know what Sleep(...) does
但我不知道 Sleep(...) 做了什么
回答by Seth
On the mac, under OSX, there is no such symbol.
在 Mac 上,在 OSX 下,没有这样的符号。
I don't think there is such a symbol in Classic mac either - I even looked in my ancient copy of THINK Reference.
我认为 Classic mac 中也没有这样的符号 - 我什至查看了我的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.
我也会惊讶地发现 C 中有一个Sleep
(大写的 S)函数,因为很多人都使用小写字母来命名 C 函数。
Were you prompted to ask the question because you're getting a link error?
您是否因为收到链接错误而被提示提问?
回答by ValiRossi
There is usleep().
有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;