Re: Event loop and sleep()

From: Xenos (dont.spam.me_at_spamhate.com)
Date: 07/02/04


Date: Fri, 2 Jul 2004 09:16:12 -0400


"Alo Sarv" <alo17@hot.ee> wrote in message
news:4a757817.0407012233.5b08636a@posting.google.com...
> "Xenos" <dont.spam.me@spamhate.com> wrote in message
news:<cc1d2l$nsc6@cui1.lmms.lmco.com>...
> > "Howard" <alicebt@hotmail.com> wrote in message
> > news:YeWEc.171934$Gx4.94253@bgtnsc04-news.ops.worldnet.att.net...
> > >
> > > "Claudio Puviani" <puviani@hotmail.com> wrote in message
> > > news:_9TEc.60902$OT6.24526687@news4.srv.hcvlny.cv.net...
> > > > "Alo Sarv" <alo17@hot.ee> wrote
> > > >
> > > > There's no reason whatsoever to have a sleep() in there. On any but
the
> > most
> > > > absurdly written event-handling libraries, either the operation that
> > gets
> > an
> > > > event is a blocking call, or the event multiplexer is. The loop
should
> > simply be
> > > > getEvent/handleEvent with no arbitrary delay between the two.
> > > >
> > >
> > > Yes, there is a reason. It's put there to reduce the priority of the
> > > process such that other processes can run their event loops. Without
the
> > > sleep there (at least on Windows, which you might be referring to as
one
> > of
> > > those absurdly-written libraries(?)), the loop hogs all the processing
> > > power, making the system unresponsive. Try the loop with and without
the
> > > sleep in a Windows system (if you've got one), and you should be able
to
> > see
> > > what I mean. (I don't have any idea about other systems, though.)
> > >
> > > -Howard
> > >
> > >
> >
> > sleep does not change the priority of a process in Window, nor in any
other
> > system I know of.
> >
> > DrX
>
> I think I failed to make my intentions clear, so let me clarify:
>
> The application is singlethreaded, all events are received from
> sockets. The purpose of sleep() was to wake up at some interval, check
> if there is anything to process from sockets, and then go back to
> sleep. I don't see a way to make the system blocking without coupling
> event engine directly with sockets. Additionally, there might pending
> jobs that take long time (checksumming files for example), that should
> be done in small amounts (64k blocks for example) during event loops.
> Or there could be jobs that need to be done at specific intervals (for
> example, autosaving config file every 20 minutes). Thus blocking
> the loop if there are no socket events isn't an option, since that
> would also block those jobs. Removing the sleep() from the loop causes
> the loop to run as fast as possible, thus using 100% CPU (on any
> system/compiler).
>
> One option that was pointed out in this thread was to loop around
> until there are events to process, and then go to sleep for a minimum
> delay. Now, if an event handler function posts a new event, that event
> also needs to be processed. And if a handler for the new event also
> posts a event, that one also needs to be processed, thus can cause
> endless loop if the user of the event system isn't careful... Ok, this
> could be countered by limiting the number of runs through event loop
> w/o sleeping to some amount, after which a sleep will be forced. Can't
> say I'm exactly thrilled with this solution.
>
> Alo.

Although this thread is a bit off-topic, there are many more ways to make a
thread block then just sockets. As someone suggested, the message queue
itself can cause the thread to block when its empty. If that's not already
a property of the queue, the funtionally can be achieved by wrapping the
read of the queue with the taking of a semaphore, and the write with the
giving the semaphore (text book reader-writer mechanism). Artifically
blocking via delays is, in my humble opinion usually bad. The event to be
handled is decoupled and asynchronous to the thead of execution that is
process it. It is always best if the two can be tied together so that it's
the event the awakens the task. The task will not waste time sleeping when
there are events to process and it will not waste the processor polling for
events when they are not there.



Relevant Pages

  • Re: Event loop and sleep()
    ... It's put there to reduce the priority of the ... Try the loop with and without the ... The purpose of sleep() was to wake up at some interval, ... if there is anything to process from sockets, ...
    (comp.lang.cpp)
  • Re: Event loop and sleep()
    ... It's put there to reduce the priority of the ... Try the loop with and without the ... The purpose of sleep() was to wake up at some interval, ... if there is anything to process from sockets, ...
    (comp.lang.cpp)
  • Re: SYSV message queues
    ... > message queue and goes through it in a loop, ... > that inserts various messages into the queue. ... > one run and decide on the sleep interval. ... Did you try to grok what the msgrcv() call is supposed to return? ...
    (freebsd-hackers)
  • cant get out of infinite while loop
    ... To get into the infinite loop, I write to a file the "on" status ... writing switch on ... ready to sleep for 10 seconds ... or die "Problem passing files to the bridge directory...$!"; ...
    (comp.lang.perl.misc)
  • Re: question about thread scheduling
    ... I doubt you will need to mess with the system tick to get what ... I will try what you suggested, the reason that I didn't use the sleep ... If the NN run in a different thread as the control loop ... Sleepputs your thread to sleep for 3 timer ticks and ...
    (microsoft.public.windowsce.platbuilder)