开发者

Where is msleep declared?

The source from here says that it is supposed to work on the iPhone. I have worked with it, but I get 2 errors, saying that msleep() is undeclared. I have tried to include unistd.h, time.h,开发者_运维问答 and numerous others. How can I get this to work? Thanks.


The msleep() is a non-standard artifact from early BSDs, before the clock_nanosleep() and nanosleep() made it into POSIX.

It is unportable. On some systems it is available by default - on others one has to compile the code with _BSD_SOURCE define.

iPhone is a distant relative of Mac OS X, which is distant relative of NeXT, which is very distant relative of BSD 4.x. So the function might have stuck in some header/library somewhere, but you shouldn't use it anyway. If memory serves me right, check the NSThread's sleepForTimeInterval: static method.


There is nothing in that linked thread stating that msleep is available. The original author, bagusflyer, actually implemented their own msleep, stating:

Sorry. Maybe I missed something in my code. Here is my msleep:

#include <sys/time.h>
void msleep (unsigned int ms) {
    int microsecs;
    struct timeval tv;
    microsecs = ms * 1000;
    tv.tv_sec  = microsecs / 1000000;
    tv.tv_usec = microsecs % 1000000;
    select (0, NULL, NULL, NULL, &tv);  
}

However, you should be careful about using that code since I think, from memory, that select() is interruptable.


Maybe you can use usleep(). It is also in unistd.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜