Article # 240, added by Geoworks, historical record
| first | previous | index | next | last |

How to safely synchronize threads accessing shared data



If your application or library provides data that will be accessed 
by multiple threads, you should use semaphores to synchronize data
access.

First you must create the semaphore that will be used to control
access.  This is done with ThreadAllocSem().  Pass the value with
which to initialize the semaphore; this value represents the number
of threads that can grab the semaphore before other grab attempts 
will block.  Typically, the passed value will be one.

You then use ThreadPSem() to attempt to gain access to the semaphore.
The thread will block until access is granted.  

ThreadVSem() is used to release access.

For more information, see Concepts book, appendix B.5 "Synchronizing
Threads"