Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ashoksaravanan
Beginner
86 Views

Thread Safety

What does the term thread-safe exactly mean? Pls explain..
0 Kudos
6 Replies
TimP
Black Belt
86 Views

Much good material comes right up in any web search.
jimdempseyatthecove
Black Belt
86 Views

Ashok,

Generally thread-safe means your code will not break due to unexpected changes to variables that are shared between threads but where the code was never written with consideration that multiple threads may concurrently be executing the code.

A good and simple example of this would be code that manages a linked list. The extraction might look like:

node = head;
if(node) head = node.next;

This looks safe enough but what if thread A picks up the head node, then before updating the head pointer thread B inserts a new head node, then thread A updates the head node with node.next. Under this circumstance the node inserted by B would be lost.

This two line code example is not thread safe. The window of error opportunity is quite small in this code exampleand may not be noticed for a long time. And may not be noticed during debugging. It might only occure once the code passes verification test and gets into the field.

Jim Dempsey

robew
Beginner
86 Views

Hello


Its not much complecated. visit here to know about thread safety
SergeyKostrov
Valued Contributor II
86 Views

What does the term thread-safe exactly mean? Pls explain..


Consider a financial software that does one transaction in a multi-threaded environment ( many users are
using software at the same time ):

Transfer $1 from AccountA to AccountB.

It will be athread-safetransaction onlyin one case if the transaction is not interrupted until it is completed.
A pseudo-code could look like:

ISOLATION LEVEL: Set to Exclusive
BEGIN TRANSACTION
AccountA: -$1
AccountB: +$1
COMMIT if everything is OK, or ROLLBACK in case of an error
ISOLATION LEVEL: Set to Shared

That scheme guarantees that the balance of a financial organizationis correct at the end of thetransaction. In
overall, some set of synchronization objects has to be used, like mutexes, critical sections, events, or
semaphors, to isolate some piece of codeor adatabase datafrom simultaneous modifications from different threads ( users ).

Best regards,
Sergey

Christopher_K_
Beginner
86 Views

Practicing thread-safety is the process of writing source code that is multi-thread safe; meaning, the software can perform more than one action at a time without crashing. The theory and practice can be a bit complicated, depending on the language you are using. C/C++ has multiple libraries available to introduce threading capability, while C# and Java were designed to be thread-safe from the ground up. Hope this helps.

CCK
jimdempseyatthecove
Black Belt
86 Views

>>the software can perform more than one action at a time without crashing.

The preponderance of effects of un-thread-safe programming is incorrect results - not program crash.

In many cases you will find the probability of error so low that errors in output is not noticed during development testing, only to find later during production that such an error exists. An un-seasoned developer will assume a position of: "this cannot possibly be a coding error on my part".

Some thread-unsafe coding may never exhibit an error, or will not show an error for years of operation. While thread-safety issues can be exposed by riggorous stress testing and/or by modern tools and practices, you should not become lax and avoid the thurough walk-through of your code with the comprehension of what the other threads (and activity) may be doing at the time of execution of the code under review. Keeping in mind that the code you see in source form is not the code the computer sees at run time.

Jim Dempsey
Reply