Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing a Thread Safe Class

When reading the MSDN documentation it always lets you know if a class is thread safe or not. My question is how do you design a class to be thread safe? I am not talking about calling the class with locking I am meaning I am working for Microsoft create XXX class\object and I want to be say it is "Thread Safe" what would I need to do?

like image 761
Shane Avatar asked Jun 27 '10 20:06

Shane


People also ask

What are thread-safe classes?

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

How do I make a swift class thread-safe?

By creating a queue of tasks where only one task can be processed at any given time, you are indirectly introducing thread safety to the component that is using the queue.


2 Answers

The easiest and most foolproof way of making a class thread safe is to make it immutable. The beauty of it is that you don't ever have to bother with locking again.

Recipe: Make all instance variables readonly in C# (final in Java).

  • An immutable object, once created and initialized in the constructor, cannot change.
  • An immutable object is thread safe. Period.
  • This is not the same as having a class with only constants.
  • For mutable parts of your system, you still need to account for and handle locking/synchronization property. This is one reason to write immutable classes in the first place.

See this question as well.

like image 72
Martin Wickman Avatar answered Sep 28 '22 05:09

Martin Wickman


In addition to the other excellent answers here, consider another angle as well.

It isn't enough that the internal data structure of the class is 100% thread safe if the public API has multi-step operations that cannot be used in a thread-safe manner.

Consider a list class that has been built such that no matter how many threads are doing no matter how many types of operations on it, the internal data structure of the list will always be consistent and OK.

Consider this code:

if (list.Count > 0)
{
    var item = list[0];
}

The problem here is that between the reading of the Count property and the reading of the first element through the [0] indexer, another thread might have cleared out the contents of the list.

This type of thread safety is usually forgotten when the public API is created. Here, the only solution is for the calling code to manually lock on something on each such type of access to prevent the code from crashing.

One way to solve this would be for the list type author to consider typical usage scenarios and add the appropriate methods to the type:

public bool TryGetFirstElement(out T element)

then you would have:

T element;
if (list.TryGetFirstElement(out element))
{
    ....

presumably, TryGetFirstElement works in a thread-safe manner, and would never return true at the same time as it is not able to read the first element value.

like image 36
Lasse V. Karlsen Avatar answered Sep 28 '22 05:09

Lasse V. Karlsen