TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Paul Neth
from: Jonathan de Boyne Pollard
date: 1995-12-29 14:27:12
subject: Why not use timers?

PN>
  > Could someone explain why you dont want to use the
  > Winstarttimer in a PM thread, versus dossleep to provide
  > polling?
PN>

  Two reasons :

  The first reason is that responding to WM_TIMER messages can reduce
  the responsiveness of the user interface.  You don't want to be doing
  significant processing in response to any PM message, because that
  causes the user interface to "hang".  This, in turn, mandates that in
  response to a WM_TIMER message you kick a secondary thread into action
  (either with _beginthread or by posting an event semaphore).

  In which case, you have to ask yourself why you are going to the
  expense of using a system timer and a semaphore at all, when coding
  the thread as a loop with a simple DosSleep() call in the middle will
  achieve the same effect.

  The second reason is Peter Fitzsimmons' Thread Maxim : "A thread must
  do nothing well.".  In other words, if a thread has nothing to do, it
  must ensure that it uses as little CPU time as possible. This is why
  polling is a dirty word in OS/2 programming.

  If the thread is simply a "do X regularly" thread (i.e. the thread
  isn't polling for some external event to happen), then the

      for (;;) {
          do_something() ;
          DosSleep( some_interval ) ;
      }

  design is pretty much the most efficient design (and, luckily, also
  the simplest).

  ( Exiting the program can be made neater if the thread uses a timed
  wait on a semaphore rather than DosSleep() (since the main thread can
  cause all secondary threads to wake up immediately by posting their
  semaphores), but the basic design remains the same. Many people would
  argue that any such thread that *couldn't* be terminated while
  sleeping is an improperly designed thread, but that is a separate
  issue.  )

  If, however, there is a system service available to block the thread
  until the required event occurs, we can improve the design. The best
  design for a thread that responds to external events would not use
  DosSleep() at all.

  There's no point in coding

      for (;;) {
          if (poll_something())
              respond_to_something() ;
          DosSleep ( some_interval ) ;
      }

  instead of

      for (;;) {
          make_a_system_call_to_block_until_something_happens() ;
          respond_to_something() ;
      }

  because the former consumes CPU time even when there is nothing to do.

  There are other advantages of the latter over the former, as well (the
  latter is more responsive to events, for example -- see the our
  October 1994 discussion on this subject that I've just re-posted for
  more details).

  > JdeBP <
___
 X MegaMail 2.10 #0:
--- Maximus/2 3.00
* Origin: DoNoR/2,Woking UK (44-1483-725167) (2:440/4)
SEEN-BY: 270/101 620/243 711/401 409 410 413 430 808 809 934 955 712/407 515
SEEN-BY: 712/517 628 713/888 800/1 7877/2809
@PATH: 440/4 141/209 270/101 712/515 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.