TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Jari Laaksonen
from: Mark Kimes
date: 1994-09-12 21:57:22
subject: Re: threads, threads, mor

jl>Now I have a dynamically allocated structure and thread functions
 jl>use a global pointer to that structure. One thread usually modifies
 jl>some members in the global structure, e.g. some counters, while
 jl>other threads just read these counters.

 jl>I have several examples (from Toolkit, EDMI and EMEA DAP Library)
 jl>of multiple threads in a PM program and some of them use semaphores
 jl>and some don't. I'm not very familiar with semaphores, yet.

At the start of your program, create a mutex (mutual exclusion)
semaphore to protect the global structure (see DosCreateMutexSem in the
toolkit docs).  Then, before modifying or reading anything in the
structure, request the mutex semaphore (see DosRequestMutexSem in the
toolkit docs).  Once the semaphore is obtained, do what you need with
the structure, then release the mutex semaphore (see DosReleaseMutexSem
in the toolkit docs).

In this way, only one thread at a time is actually accessing the global
structure, and only threads actually trying to get at the structure are
ever blocked (and, of course, only if they're trying to get at it when
another thread already owns the semaphore).  Here's some brief code
fragments to help you along:

HMTX GlobalSem;  /* the semaphore that will protect global struct */

...

if(DosCreateMutexSem(NULL,&GlobalSem,0L,FALSE))
  /* failed, bomb out */

...

if(DosRequestMutexSem(GlobalSem,SEM_INDEFINITE_WAIT))
  /* failed, bomb out */
else { /* any other threads trying to access global struct are now blocked */
  /* manipulate (read or write) global struct, then */
  DosReleaseMutexSem(GlobalSem);
}

--- XHEd-OS/2 1.22

* Origin: The Pit (1:380/16)
SEEN-BY: 12/2442 54/54 620/243 624/50 632/348 640/820 690/660 711/409 410 413
SEEN-BY: 711/430 807 808 809 934 942 712/353 623 713/888 800/1
@PATH: 275/34 1 3615/50 229/2 12/2442 711/409 54/54 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™.