为什么要使用选择()而不是睡眠()? [英] Why use select() instead of sleep()?

查看:169
本文介绍了为什么要使用选择()而不是睡眠()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个有关iPhone的音频章节工作和所遇到的code的部分,我不能做的意义:

I'm working through a chapter about iPhone audio and have come across a section of code that I can't make sense of:

while (aqc.playPtr < aqc.sampleLen) 
{
    select(NULL, NULL, NULL, NULL, 1.0);
}

全部code样品在页面163-166 )。从我了解的code中的声音正在被另一个线程处理,while循环只是在那里prevent从而仍在处理中的音频终止主线程。

(Full code sample is on pages 163-166). From what I understand of the code the audio is being processed on another thread and the while loop is just there to prevent the main thread from terminating while audio is still being processed.

我不明白的是为什么选择()被用来代替睡眠()

What I don't understand is why select() is being used instead of sleep().

这是我读过选择()用来监测I / O的变化,并通过它为NULL没有做任何有意义。我已经跑了使用code 睡眠(),它按预期工作。 (我的水平低POSIX的知识几乎是不存在的。)

From what I've read select() is used to monitor changes in I/O and passing it NULLs doesn't do anything meaningful. I've ran the code using sleep() and it works as expected. (My knowledge of low level POSIX is almost non-existant.)

推荐答案

选择允许精确的亚秒的等待,比睡眠更便于携带。还有其他的方法来等待,看看这个问题

Select allow for accurate sub second wait, and is more portable than sleep. There are other ways to wait, see this question.

但选择的超时参数不应该是一个浮动,但一个指向timeval结构。我很惊讶的code你看连编译。更多信息:这个奇怪的条件选择后跟一个unconditionnal睡眠(1)。看起来没有意义的我。

But the timeout parameter of select should not be a float but a pointer to struct timeval. I'm surprised the code you show even compiles. More : this strange conditional select is followed by an unconditionnal sleep(1). Looks pointless to me.

这篇关于为什么要使用选择()而不是睡眠()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆