Thread Safe

Code is ThreadSafe if it has the desired result when multiple threads are executing it concurrently.

How can this be achieved ?

By using semaphores and locks so only one thread is modifying any piece of data at a time, and no reads occur during writes.

Or, write in a ThreadSafeLanguage.

Ways to make code ThreadSafe in a thread-vicious language:

Note for the unwary: for those used to coding on PCs, which typically have a single processor, there can be a tendency to cheat a little bit, knowing that the CPU, in reality, can only do one thing at a time.

I wrote a multi-threaded serial data acquisition driver that was "ThreadSafe" -- as long as it only ran on one CPU.

When we deployed this driver (after three solid months of testing without a failure) it broke in the first two hours of use -- the target box had multiple processors. I was at the client's site until 2:00am that night recoding the locking mechanisms to eliminate PriorityInversion, RaceCondition, and FatalEmbrace? problems that never showed up on a single processor.

Safety = always assume that a condition of truly simultaneous access can occur and code accordingly.

-- GarryHamilton


EditText of this page (last edited February 11, 2004) or FindPage with title or text search