| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | WaitForMessage(); |
ZZ>
> I'm currently using a WaitForMessage() routine
> where in the routine I simply give up time slices to os/2
> (This is in the dos version) and check for mouse activity,
> and keyboard activity... now I know there is a better way
> to do it through os/2 , but I have yet to learn it...
ZZ>
___-------------------------------------------------------------------------
Conference: Os2prog
To: Phil Crown Date: 10/22/1994
From: YOU Time: 9:51 am
PC>
> while( 1 ) {
> rc = KbdCharIn( &ki, 1, 0 );
> DosSleep( 5 ); // give up time slices this drops the CPU usage from
> // %100 to %5 in this example
do_mouse_and_clock_stuff() ;
> }
PC>
Since you asked how else a program would be able to know when a key on
the keyboard is hit, here's my multithreaded improvement on the above
single-threaded design.
Main thread :
Initialise event_queue and start secondary threads ;
for (;;) {
event = event_queue.blocking_read () ;
switch (event) {
case MOUSE : do_mouse(event) ; break ;
case KEYBOARD: do_keyboard(event) ; break ;
}
}
So when there is no work to do this thread spends its time
blocked reading the event queue.
Secondary thread 1 :
for (;;) {
mouse_event = blocking_read_of_mouse () ;
event_queue.post(mouse_event) ;
}
So when there is no work to do this thread spends its time
blocked in the upper half of the mouse device driver.
Secondary thread 2 :
for (;;) {
keyboard_event = blocking_read_of_keyboard () ;
event_queue.post(keyboard_event) ;
}
So when there is no work to do this thread spends its time
blocked in the upper half of the keyboard device driver.
Secondary thread 3 :
for (;;) {
DosSleep(1000) ;
update_clock_display () ;
}
This thread spends all of its idle time blocked within DosSleep() .
Total CPU usage when there is no actual work to do ? Well, since all
threads are blocked, it must be zero.
( Incidentally, Peter Fitzsimmons will tell you that you can also page
tune the program by putting the inner loops of the threads as close
together as possible, reducing the working set of an idle process. )
The only (slight) difficulty for the programmer is the event queue.
This must support blocking reads, and all calls must be thread-safe.
Although there is a message queue IPC facility provided by OS/2, it's
possibly more expensive than rolling your own, since it has to use
shared memory to ensure communication between processes. For multiple
threads within a single process, a global data structure can be used,
and the only problem is synchronisation. Fortunately, this happens to
be the standard producer/consumer problem from most textbooks, and is
easily solved using semaphores.
Do you think that David Hamel is listening ? (-:
> JdeBP <
___ Maximus/2 2.01wb
___----------------------------------------------------------------------------
> JdeBP <
---
X MegaMail 2.10 #0:
--- Maximus/2 2.02
* Origin: DoNoR/2,Woking UK (0483-725167) (2:440/4)* Origin: DoNoR/2,Woking UK (44-1483-725167) (2:440/4) SEEN-BY: 105/42 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407 SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809 @PATH: 440/4 141/209 270/101 105/103 42 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™.