Skip to content

Latest commit

 

History

History
40 lines (26 loc) · 2.39 KB

Thread-Local Storage.md

File metadata and controls

40 lines (26 loc) · 2.39 KB

Thread-Local Storage

Simple Explanation

Thread-local storage (TLS) is a mechanism in multi-threaded programming that allows each thread to have its own private storage for data. In C# and .NET, the ThreadLocal class provides an easy way to create and manage thread-local storage.

Deep Explanation

In multi-threaded programming, it's often useful to have a way for each thread to have its own private storage for data. This can help avoid race conditions and other synchronization issues that can arise when multiple threads access the same shared data.

Thread-local storage (TLS) is a mechanism that provides this functionality. TLS allows each thread to have its own private storage for data, which is not accessible by other threads. This way, each thread can manipulate its own private copy of the data without affecting the copies held by other threads.

In C# and .NET, the ThreadLocal class provides a convenient way to create and manage thread-local storage. When you create a ThreadLocal instance, you can specify the type of data that you want to store in the thread-local storage. Each thread that accesses the ThreadLocal instance gets its own private copy of the data.

The ThreadLocal class provides several constructors and methods that allow you to customize the behavior of the thread-local storage. For example, you can specify a default value for the data, or you can provide a factory function that creates a new instance of the data for each thread.

Examples

public class MyClass
{
    private ThreadLocal<int> _counter = new ThreadLocal<int>(() => 0);

    public void IncrementCounter()
    {
        _counter.Value++;
    }

    public int GetCounterValue()
    {
        return _counter.Value;
    }
}

In this example, we define a MyClass class that has a private _counter field of type ThreadLocal. We initialize the _counter field with a factory function that creates a new instance of the int data type with an initial value of zero for each thread that accesses it.

The IncrementCounter method increments the value of the _counter field for the current thread, and the GetCounterValue method returns the current value of the _counter field for the current thread.

By using the ThreadLocal class, we ensure that each thread has its own private copy of the _counter field, and that changes made to the field by one thread do not affect the copies held by other threads.