Method lock in c#

30,616

Solution 1

If I understood correctly, you need something like this:

static object lockMethod2 = new object();
static object lockMethod3 = new object();

public static void Method1() 
{
    lock (lockMethod2)
    lock (lockMethod3)
    {
        //Body function
    }
}

public static void Method2() 
{
    lock (lockMethod2)
    {
        //Body function
    }
}

public static void Method3() 
{
    lock (lockMethod3)
    {
        //Body function
    }
}

This allows method3 to execute if method2 is running and vice versa, while method1 must wait for both. Of course, method2 and 3 will not run while 1 is running.

Solution 2

The current implementation of your lock is completely useless, because every thread will lock on a different object.
Locking is usually done with a readonly field that is initialized only once.
Like this, you can easily lock multiple methods:

public class Class1
{
    private static readonly object _syncRoot = new object();

    public static void Method1() 
    {
        lock (_syncRoot)
        {
            //Body function
        }
    }

    public static void Method2() 
    {
        lock (_syncRoot)
        {
            //Body function
        }
    }

    public static void Method3() 
    {
        lock (_syncRoot)
        {
            //Body function
        }
    }
}

Solution 3

I would suggest a ReaderWriterLockSlim (http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx)

Similar to read operations, Method 2 and Method3 may occur in parallel, while Method1 (like a write operation) would need to wait for those to finish. It's not the regular read/write concurrency situation, but the logic is similar.

public class Class1
{
    private ReaderWriterLockSlim methodLock = new ReaderWriterLockSlim();
    public static void Method1() 
    {
        methodLock.EnterWriteLock();
        try
        {
            //Body function
        }
        finally
        {
            methodLock.ExitWriteLock();
        }
    }

    public static void Method2() 
    {
         methodLock.EnterReadLock();
        try
        {
            //Body function
        }
        finally
        {
            methodLock.ExitReadLock();
        }
    }

    public static void Method3() 
    {
         methodLock.EnterReadLock();
        try
        {
            //Body function
        }
        finally
        {
            methodLock.ExitReadLock();
        }
    }
}

Solution 4

If you are multi-threading then the lock has to be accessible to all threads. Therefore, in this case, your locks needs to be static for the static methods to see it.

Your current setup will make a new lock object for each thread. Therefore, providing now synchronization.

Share:
30,616
Emanuele Mazzoni
Author by

Emanuele Mazzoni

Updated on July 25, 2022

Comments

  • Emanuele Mazzoni
    Emanuele Mazzoni almost 2 years

    I have one class with these three methods. This class is used by many threads. I would like the Method1 to wait, if Method2 and/or Method3 are running in any threads. Any suggestions?

    public class Class1
    {
        public static void Method1() 
        {
            Object lockThis = new Object();
    
            lock (lockThis)
            {
                //Body function
            }
        }
    
        public static void Method2() 
        {
             //Body function
        }
    
        public static void Method3() 
        {
             //Body function
        }
    }
    
  • Nilay Vishwakarma
    Nilay Vishwakarma over 3 years
    What if Method1 is running parallel? And you need all the threads to access method1?