TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: All
from: Alan Clifford
date: 1996-03-19 23:03:10
subject: Threads

Hello again All

Following on my own message:

 AC> I was expecting thread 2 to print a few lines then swap to thread 3,
 AC> print a few lines and so on.  But it can get stuck in one of the
 AC> threads for about 19 seconds.  I also opened another window and ran

 AC> void thread3(void *argp)
 AC> {
 AC>     while (1) {
 AC>         Thread2ctr = 0;
 AC>         printf("\t\tthread 3, %d\n", Thread3ctr);
 AC>         ++Thread3ctr;
 AC>     }
 AC> }

I added a _sleep2(1) to each of the threads (1 millisec wait, or whatever
the smallest number my system uses) and this produced the effect I wanted. 
The output was almost too fast to see properly when running the program
twice, with the text editor in the foreground - just one or two of lines
from each thread in each process and, to the human eye, all at the same
time.  And pressing the return key ended the program instanteously.  So why
did I have to add the _sleep2(1) to cause the thread switch?

The next thing I want to try is a mutex semaphore with each thread asking
for the semaphore at the top of the while loop releasing it at the bottom. 
I am expecting just one printf from each thread at a time.  What this is
leading up to is reading from the serial port into a buffer in one thread
and taking the character out in another - is this the right way to go?  I
have some example code where the author uses a DosEnterCritSec in the input
thread but nothing in the output thread.  Isn't that a bit dangerous?

Alan

--- FleetStreet 1.14 NR
* Origin: Alan's Point on Donor/2 (alanc{at}donor2.demon.co.uk) (2:440/4.6)
SEEN-BY: 50/99 78/0 270/101 620/243 711/401 409 410 413 430 808 809 934 955
SEEN-BY: 712/407 515 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™.