TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Alan Clifford
from: Mike Bilow
date: 1996-03-21 20:30:32
subject: Threads

Alan Clifford wrote in a message to All:

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

Your design using the mutex semaphore is clearly the right one.  A mutex
semaphore should be considered to be a component of the resource being
protected, in this case the buffer, and the code should treat it as a token
that gives permission to access the resource.  You may also want to use an
event semaphore so that the thread which inserts data can tell the thread
which extracts data that there is work to be done.

   Inserting thread:

   1. Block waiting for serial port
   2. Read data from port
   3. Request mutex semaphore for buffer
   4. Insert data in buffer
   5. Release mutex semaphore for buffer
   6. Post event semaphore
   7. Go back to step 1

   Extracting thread:

   1. Block waiting for event semaphore to post
   2. Rearm event semaphore
   3. Request mutex semaphore for buffer
   4. Extract data from buffer
   5. Release mutex semaphore for buffer
   6. Do something with data
   7. Go back to step 1
 
-- Mike


--- 
* Origin: N1BEE BBS +1 401 944 8498 V.34/V.FC/V.32bis/HST16.8 (1:323/107)
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: 323/107 170/400 396/1 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™.