SoFunction
Updated on 2025-03-08

C# What problems may cause frequent access to the same resource in multithreads

What problems may arise when multiple threads frequently access the same resource

Race Conditions and Deadlocks

If two or more threads access the same object, or access an out-of-sync shared state, race conditions will occur;

To avoid this problem, shared objects can be locked. But too many locks can also cause trouble, that is deadlock;

When at least two threads are suspended, wait for the other party to unlock. Since both threads are waiting for each other, a deadlock appears and the thread will wait infinitely;

To avoid synchronization issues, it is best not to share data between threads. Of course, this is not always possible. If you need to share data, you must use synchronization technology;

Make sure that only one thread accesses and changes the shared state at a time. Note that synchronization issues are related to race conditions and deadlocks. If you don't pay attention to these problems, it will be difficult to find the cause of the problem in the application, because threading problems occur from time to time.

When multiple threads access an instance object at the same time, a lock can be added to the process.

Lock is to ensure that when one thread is in the critical section of the code, another thread does not enter the critical section.

If other threads try to enter locked code, it will wait (i.e. blocked) until the object is released.

public class Singleton
{
    private static Singleton instance;
    private static readonly object synRoot=new object();
    private Singleton() //Replace it to private    {
    }
    public static Singleton GetInstance()
    {
        lock(synRoot)
        {
            if(instance==null)
            {
                instance=new Singleton();
            }
            return instance;
        }
    }
}

Double lock: There is no need to let threads lock every time, but just lock the instance when it is not created, which improves performance.

public class Singleton
{
    private static Singleton instance;
    private static readonly object synRoot=new object();
    private Singleton() //Replace it to private    {
    }
    public static Singleton GetInstance()
    {
        if(instance==null)
        {
           lock(synRoot)
            {
                if(instance==null)
                {
                    instance=new Singleton();
                }
                return instance;
            }
        }
    }
}

Multithreaded access resource conflict problem

When multi-threading accesses the same resource, the synchronization mechanism can be used to solve the problem.

private Object _lock = new Object();//Define an objectprivate void method( paramtype,paramvalue) //Thread access method name{
lock (_lock)
{
//Conflict code}
}

1. _lock was locked? If not, the current thread will lock it, otherwise it will wait until _lock is released.

2. After lock, other threads cannot call the code in lock{}{, nor can they use _lock.

3. After executing the code in lock{}, release _lock, and the code in lock{} can be accessed by other threads.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.