Sample code to illustrate a deadlock by using lock(this)

前端 未结 6 1164
我在风中等你
我在风中等你 2020-12-23 20:57

I\'ve read several articles and posts that say that lock(this), lock(typeof(MyType)), lock(\"a string\") are all bad practice because

6条回答
  •  梦毁少年i
    2020-12-23 21:48

    This is pretty standard bad-ness. Grabing the locks out of order and then sleeping with the lock. Two bad things to do. :)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace DeadLock
    {
        public class Program
        {
            static void Main(string[] args)
            {
                var ddt = new DontDoThat();
    
                ddt.Go();
            }
        }
    
        public class DontDoThat
        {
            private int _badSharedState = 0;
            private readonly object _lock1 = new object();
            private readonly object _lock2 = new object();
    
            public void Go()
            {
                new Thread(BadGuy1).Start();
                new Thread(BadGuy2).Start();
    
                Console.WriteLine("Leaving Go!");
            }
    
            public void BadGuy1()
            {
                lock (_lock1)
                {
                    Thread.Sleep(100); // yeild with the lock is bad
                    lock (_lock2)
                    {
                        _badSharedState++;
                        Console.Write("From Bad Guy #1: {0})", _badSharedState );
                    }
                }
            }
            public void BadGuy2()
            {
                lock (_lock2)
                {
                    lock (_lock1)
                    {
                        _badSharedState++;
                        Console.Write("From Bad Guy #2: {0})", _badSharedState);
                    }
                }
            }
        }
    }
    

提交回复
热议问题