I'm working through a chapter about iPhone audio and have come across a section of code that I can't make sense of:
我正在编写一个关于iPhone音频的章节,并且遇到了一段我无法理解的代码:
while (aqc.playPtr < aqc.sampleLen)
{
select(NULL, NULL, NULL, NULL, 1.0);
}
(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.
(完整代码示例在第163-166页)。根据我对代码的理解,音频正在另一个线程上处理,而while循环就是为了防止主线程在音频仍在处理时终止。
What I don't understand is why select()
is being used instead of sleep()
.
我不明白为什么使用select()而不是sleep()。
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()用于监视I / O的变化并传递它.NULLs没有做任何有意义的事情。我使用sleep()运行代码,它按预期工作。 (我对低级别POSIX的了解几乎不存在。)
5 个解决方案
#1
18
Select allow for accurate sub second wait, and is more portable than sleep. There are other ways to wait, see this question.
选择允许准确的次秒等待,并且比睡眠更便携。还有其他方法可以等待,请看这个问题。
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 unconditional sleep(1). Looks pointless to me.
但是select的timeout参数不应该是float,而是指向struct timeval的指针。我对你展示的代码甚至编译感到惊讶。更多:这个奇怪的条件选择之后是无条件睡眠(1)。对我来说看起来毫无意义。
#2
10
Using select()
with NULL
rfds
, wfds
and efds
is an idiomatic way of portably sleeping with subsecond resolution.
使用带有NULL rfds的select(),wfds和efds是以亚秒级分辨率轻松睡眠的惯用方式。
#3
5
Well, sleep(3) may be implemented by using signals. It depends on the platform.
那么,睡眠(3)可以通过使用信号来实现。这取决于平台。
When you use select(2) and poll(2), you know that no signals will be involved, which is often very useful. For example, if you are using alarm(2), you should not use sleep(3) as well, because "mixing calls to alarm and sleep is a bad idea" (according to the man page.)
当您使用select(2)和poll(2)时,您知道不会涉及任何信号,这通常非常有用。例如,如果你正在使用alarm(2),你也不应该使用sleep(3),因为“混合调用警报和睡眠是一个坏主意”(根据手册页。)
Also, select and poll give you millisecond granularity when sleeping, but sleep only has a granularity in terms of seconds.
此外,select和poll在睡眠时会给你毫秒级的粒度,但是睡眠只有几秒钟的粒度。
#4
3
When you use SIGALM signal in your application and you use (u)sleep
functions, when SIGALRM occurs program immediately return from sleep function, so the best way to sleep is to wait on select
function.
当您在应用程序中使用SIGALM信号并使用(u)睡眠功能时,当SIGALRM发生时程序立即从睡眠功能返回,因此最好的睡眠方式是等待选择功能。
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 1000;
do
{
ret = select(1, NULL, NULL, NULL, &tv);
}
while((ret == -1)&&(errno == EINTR)); //select is interruped too
#5
-2
There's no reason to do it. There's no reason ever to Sleep() either. One should always be expecting at least one event - program shutdown request.
没有理由这样做。也没有理由睡觉()。一个人应该总是期待至少一个事件 - 程序关闭请求。
#1
18
Select allow for accurate sub second wait, and is more portable than sleep. There are other ways to wait, see this question.
选择允许准确的次秒等待,并且比睡眠更便携。还有其他方法可以等待,请看这个问题。
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 unconditional sleep(1). Looks pointless to me.
但是select的timeout参数不应该是float,而是指向struct timeval的指针。我对你展示的代码甚至编译感到惊讶。更多:这个奇怪的条件选择之后是无条件睡眠(1)。对我来说看起来毫无意义。
#2
10
Using select()
with NULL
rfds
, wfds
and efds
is an idiomatic way of portably sleeping with subsecond resolution.
使用带有NULL rfds的select(),wfds和efds是以亚秒级分辨率轻松睡眠的惯用方式。
#3
5
Well, sleep(3) may be implemented by using signals. It depends on the platform.
那么,睡眠(3)可以通过使用信号来实现。这取决于平台。
When you use select(2) and poll(2), you know that no signals will be involved, which is often very useful. For example, if you are using alarm(2), you should not use sleep(3) as well, because "mixing calls to alarm and sleep is a bad idea" (according to the man page.)
当您使用select(2)和poll(2)时,您知道不会涉及任何信号,这通常非常有用。例如,如果你正在使用alarm(2),你也不应该使用sleep(3),因为“混合调用警报和睡眠是一个坏主意”(根据手册页。)
Also, select and poll give you millisecond granularity when sleeping, but sleep only has a granularity in terms of seconds.
此外,select和poll在睡眠时会给你毫秒级的粒度,但是睡眠只有几秒钟的粒度。
#4
3
When you use SIGALM signal in your application and you use (u)sleep
functions, when SIGALRM occurs program immediately return from sleep function, so the best way to sleep is to wait on select
function.
当您在应用程序中使用SIGALM信号并使用(u)睡眠功能时,当SIGALRM发生时程序立即从睡眠功能返回,因此最好的睡眠方式是等待选择功能。
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 1000;
do
{
ret = select(1, NULL, NULL, NULL, &tv);
}
while((ret == -1)&&(errno == EINTR)); //select is interruped too
#5
-2
There's no reason to do it. There's no reason ever to Sleep() either. One should always be expecting at least one event - program shutdown request.
没有理由这样做。也没有理由睡觉()。一个人应该总是期待至少一个事件 - 程序关闭请求。