TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Phil Crown
from: Craig Swanson
date: 1996-01-24 12:03:20
subject: thread safe

-=> Quoting Paul Edwards to Peter Fitzsimmons <=-

 PF> Is it thread safe?

 PE> I've no idea.  What is thread safe?

 PC> Good question.  I'll be watching for the answer to this... :-)

It means the code can be safely used in a multithreaded process. For
instance, it means that if two threads call into the same function at the
same time (or try to do something using the same resources at the same
time) that they will not cause corrupted results.

"Same time" in the context of a single CPU system means that
before one thread has finished, a context switch happens and another thread
enters the same code or tries to use the same resource already being used.

It is possible that OS/2 applications will sometimes execute on SMP
(symmetric multiprocessing) systems.  In this case, it is entirely possible
that "same time" will be literally true as one thread can be
running on each CPU, meaning multiple threads are literally running
simultaneously.

Making code thread-safe usually involves the following:

1) Avoid global variables

2) Use local (i.e., auto or allocated on the stack) variables

3) Protect resources that are not strictly local using mutex
   semaphores or handle them using some other scheme such as atomic
   bit test-and-set, inc/dec, and exchange operations

Making code SMP-safe takes it one step further as you must then consider
that some practices that would be safe on a single CPU system will not be
safe on a multiple CPU system.  For instance, a simple Intel x86 INC
instruction used to update a counter being handled by multiple threads
would need to have a LOCK prefix before it to make it SMP-safe.


--- Maximus/2 2.02
* Origin: OS/2 Connection {at} Mira Mesa, CA (1:202/354)
SEEN-BY: 50/99 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: 202/354 300 777 3615/50 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™.