事件循环和睡眠() [英] Event loop and sleep()

查看:98
本文介绍了事件循环和睡眠()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




从我对这个新闻组中各个帖子的理解,编写活动

循环几乎归结为:


while(true){

handleEvents();

sleep(1); //或_sleep()或nanosleep(),取决于平台

}


问题是sleep()睡眠时间至少延迟,但是可以睡觉

更长。最初我认为这不是问题,直到我进行了一些测试:


在P4 / 2.6ghz / WinXP上,它每秒运行~500次循环(2ms平均睡眠)

在P4 / 2.6ghz / Linux 2.6.5上,它每秒运行~250次循环(3ms平均休眠)

在Cel / 400mhz / Linux 2.4.20上运行〜每秒30个循环(20ms平均睡眠)


这些时间是用空闲事件循环测量的,例如没有任何活动

张贴/处理。在P4系统上,一切都很好,但是我担心低端系统上的价格是30美元。由于这是空闲循环,一旦我开始发布/处理大量事件,它就会降低甚至更低......


我''我不确定,但这不会让应用程序变得非常慢吗?除上述之外还有其他的

事件循环机制吗?我做了相当多的搜索

谷歌,但是在编写事件循环的主题上却非常热心。

也许有人可以推荐一本书/网站话题?


Alo Sarv。

Hi

From what I have understood from various posts in this newsgroup, writing event
loops pretty much comes down to this:

while (true) {
handleEvents();
sleep(1); // or _sleep() or nanosleep(), depending on platform
}

The problem is that sleep() sleeps at minimum the delay given, but may sleep
longer. Initially I thought this wasn''t a problem, until I ran some tests:

On P4/2.6ghz/WinXP, it runs ~500 loops per second ( 2ms avg sleep)
On P4/2.6ghz/Linux 2.6.5, it runs ~250 loops per second ( 3ms avg sleep)
On Cel/400mhz/Linux 2.4.20, it runs ~30 loops per second (20ms avg sleep)

These times were measured with idle event loop, e.g. no events were
posted/handled. On the P4 system, its all good, however I''m concerned about the
30 lps on low-end systems. Since this is idle looping, as soon as I start
posting/handling large amounts of events, it will drop even lower...

I''m not sure, but doesn''t that make the app extremely slow? Are there any other
event loop mechanisms besides the above? I did a fair amount of searching over
google but came up pretty emtpy-handed on the topic of writing event loops.
Perhaps someone can suggest a book / website on the topic?

Alo Sarv.

推荐答案

Alo Sarv写道:
Alo Sarv wrote:


从我对这个新闻组的各个帖子的理解,编写事件
循环几乎归结为:

while(true ){
handleEvents();
sleep(1); //或_sleep()或nanosleep(),取决于平台


问题是sleep()睡眠时间至少延迟,但可能会睡得更久。最初我认为这不是问题,直到我进行了一些测试:

在P4 / 2.6ghz / WinXP上,它每秒运行~500次循环(2ms平均睡眠)
在P4上/2.6ghz/Linux 2.6.5,每秒运行~250次循环(3ms平均休眠)
在Cel / 400mhz / Linux 2.4.20上,它每秒运行~30次循环(20ms平均睡眠)
这些时间是用空闲事件循环测量的,例如没有发布/处理任何事件。在P4系统上,这一切都很好,但我担心的是低端系统上的30 lps。由于这是空闲循环,一旦我开始发布/处理大量事件,它就会降低甚至更低......


你总是限于你的系统可以处理。虽然每秒30美元b $ b循环似乎相当低,除非在该系统上运行其他东西

。似乎在最慢的系统上,你总是会花费一个完整的时间片,如果

中的另一个进程连续以相同的优先级运行,可能会发生这种情况。如果你提高

线程优先级(仅仅是为了测试)会发生什么?


而不是睡觉你可以屈服而不是因为你不似乎是有兴趣获得一定的延迟,但只想对系统中的其他

线程感兴趣。在Win32上使用符合POSIX的

平台上的pthread_yield()或Sleep(0)。请注意,当事件循环无关时,无论是睡觉还是屈服于
都是有效的。它仍然消耗了
CPU周期并导致不必要的上下文切换。

我不确定,但这不会让应用程序变得非常慢吗?除上述之外还有其他的事件循环机制吗?我对谷歌进行了相当多的搜索,但是关于编写事件循环的话题却非常明确。
也许有人可以推荐关于这个主题的书籍/网站?
Hi

From what I have understood from various posts in this newsgroup, writing event
loops pretty much comes down to this:

while (true) {
handleEvents();
sleep(1); // or _sleep() or nanosleep(), depending on platform
}

The problem is that sleep() sleeps at minimum the delay given, but may sleep
longer. Initially I thought this wasn''t a problem, until I ran some tests:

On P4/2.6ghz/WinXP, it runs ~500 loops per second ( 2ms avg sleep)
On P4/2.6ghz/Linux 2.6.5, it runs ~250 loops per second ( 3ms avg sleep)
On Cel/400mhz/Linux 2.4.20, it runs ~30 loops per second (20ms avg sleep)

These times were measured with idle event loop, e.g. no events were
posted/handled. On the P4 system, its all good, however I''m concerned about the
30 lps on low-end systems. Since this is idle looping, as soon as I start
posting/handling large amounts of events, it will drop even lower...
You are always limited to what your system can handle. Though ony 30
loops per second seems to be rather low unless there are other things
running on that system. It appears that on the slowest system you always
loose a full time-slice, which may happen if another process in
continuosly running at the same priority. What happens if you raise the
thread priority (just for the sake of testing)?

Instead of sleeping you could yield instead since you don''t seem to be
interested in getting a certain delay, but only want to be nice to other
threads on the system. To yield use pthread_yield() on POSIX compliant
platforms) or Sleep(0) on Win32. Note that neither sleeping nor yielding
is efficient when the event loop has nothing to do. It still consumes
CPU cycles and causes unnecessary context switches.
I''m not sure, but doesn''t that make the app extremely slow? Are there any other
event loop mechanisms besides the above? I did a fair amount of searching over
google but came up pretty emtpy-handed on the topic of writing event loops.
Perhaps someone can suggest a book / website on the topic?




除了在事件循环中进行轮询和休眠之外,阻止事件循环线程的效率会更高,直到某个事件实际上已经为
到了。根据操作系统,您可以使用条件变量(POSIX)

或事件(Win32)。当事件被放入事件队列时,

您发出事件循环线程唤醒的信号。当唤醒事件循环线程

时,它会处理事件队列中的所有事件,之后它会等待另一个事件到达事件队列。

这种方法的优点是事件循环没有消耗CPU周期,因为它与b $ b无关。


请注意,您的问题(以及我的回答)与C ++

语言无关,因此在这里是偏离主题的。就像线程,睡眠和

yield函数不是C ++标准的一部分。你可以在comp.programming.threads中询问

这个问题。


-

Peter van Merkerk
peter.van.merkerk(at)dse.nl



Instead of polling and sleeping in the event loop it would be more
efficient to block the event loop thread until a event has actually
arrived. Depending on the OS you could use condition variables (POSIX)
or Events (Win32) for this. When a event is put into the event queue,
you signal the event loop thread to wake up. When the event loop thread
is awakened it processes all events in the event queue and after that it
waits for another event to arrive in the event queue. The advantage of
this approach is that the event loop doesn''t consume CPU cycles when it
has nothing to do.

Note that your question (and my answer) has nothing to do with the C++
language, and therefore is off-topic here. Thing like threads, sleep and
yield functions are not part of the C++ standard. You may want to ask
this question in comp.programming.threads.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Alo Sarv写道:
Alo Sarv wrote:
从我的内容从这个新闻组的各个帖子中了解到,编写
事件循环几乎归结为:

while(true){
handleEvents();
sleep( 1); //或_sleep()或nanosleep(),取决于平台


问题是sleep()睡眠时间至少延迟,但可能会睡得更久。最初我认为这不是问题,直到我运行了一些
测试:
From what I have understood from various posts in this newsgroup, writing
event loops pretty much comes down to this:

while (true) {
handleEvents();
sleep(1); // or _sleep() or nanosleep(), depending on platform
}

The problem is that sleep() sleeps at minimum the delay given, but may
sleep longer. Initially I thought this wasn''t a problem, until I ran some
tests:




您可以这样做:

while(true)

{

while(pendingEvents())

handleEvens();

sleep(1);

}


-

Salu2



You can do something like this:

while (true)
{
while (pendingEvents () )
handleEvens ();
sleep (1);
}

--
Salu2

=?ISO-8859-15?Q?Juli = E1n?= Albo发布:
=?ISO-8859-15?Q?Juli=E1n?= Albo posted:
Alo Sarv写道:
Alo Sarv wrote:
根据我对这个新闻组中各个帖子的理解,
编写事件循环几乎可以归结为:

while(true){
handleEvents();
睡觉(1); //或_sleep()或nanosleep(),取决于平台}

问题是sleep()睡眠时间最小,但可能会睡得更久。最初我认为这不是问题,直到我跑了
一些测试:
From what I have understood from various posts in this newsgroup,
writing event loops pretty much comes down to this:

while (true) {
handleEvents();
sleep(1); // or _sleep() or nanosleep(), depending on platform }

The problem is that sleep() sleeps at minimum the delay given, but may
sleep longer. Initially I thought this wasn''t a problem, until I ran
some tests:



你可以这样做:

while(true )
{
while(pendingEvents())
handleEvens();
sleep(1);
}



You can do something like this:

while (true)
{
while (pendingEvents () )
handleEvens ();
sleep (1);
}




for(;;)

{


}

这样就没有任何评估。


-JKop



for (;;)
{

}
That way nothing is evalutated.

-JKop


这篇关于事件循环和睡眠()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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