Method lock in c#

后端 未结 4 1962
孤城傲影
孤城傲影 2021-02-19 05:16

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 sugges

相关标签:
4条回答
  • 2021-02-19 05:36

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:43

    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
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:56

    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.

    0 讨论(0)
  • 2021-02-19 05:58

    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.

    0 讨论(0)
提交回复
热议问题