代码之家  ›  专栏  ›  技术社区  ›  ddyer

C“Sleep”函数(大写“S”)在Mac上做什么?

  •  4
  • ddyer  · 技术社区  · 14 年前

    注意大写字母“S” Sleep

    6 回复  |  直到 9 年前
        1
  •  6
  •   Josh Lee ZZ Coder    14 年前

    嗯,这是一个旧的Carbon函数(在CoreServices/OSServices框架中),它让计算机进入睡眠状态。我找不到任何文件。

        2
  •  2
  •   The Lazy Coder    12 年前

    sleep(int)是运行mac的unix系统中的一种方法,称为Darwin。

    Here is the ManPage for sleep

    本质上,它是一个C调用,让你告诉计算机睡眠'int'秒数。

    或者您可以使用“usleep(unsigned int)”,它将休眠“unsigned int”的“微秒”数,即秒*1000*1000或1000毫秒。

    Here is the ManPage for usleep

    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
    

    如果您希望拥有更高的粒度,您将需要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
    

    我希望这有帮助

        3
  •  0
  •   Preet Sangha    14 年前

    相当于睡眠的应该是

    [ NSThread sleepForTimeInterval:5.0];

    然而,这是以秒为单位的。要使用毫秒,我认为必须使用usleep(num*1000),其中num是磨坊数

    但我不知道睡眠(…)做什么

        4
  •  0
  •   Seth    14 年前

    在mac上,OSX下没有这样的符号。

    我也不认为经典mac中有这样一个符号——我甚至看了我的古老版本 THINK Reference .

    我也会惊讶地发现 Sleep

        5
  •  0
  •   ValiRossi    14 年前

    有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
  •  0
  •   Tomas    14 年前

    你在项目中使用其他库吗?

    我在使用Cocoa和Carbon使用apple的模板项目时遇到编译错误,但是我注意到sleep函数(使用定义)是SDL和SFML跨平台库中的一个特性,也许对许多其他库也是如此。

    可能是这样的 Sleep() 是链接到的其他对象中的函数。