Deadlock sample in .net?

后端 未结 5 1404
清歌不尽
清歌不尽 2020-12-16 18:07

Can anybody give a simple Deadlock sample code in c# ? And please tell the simplest way to find deadlock in your C# code sample. (May be the tool which will detect the dead

5条回答
  •  误落风尘
    2020-12-16 18:38

    one common way is if you have nested locks that aren't acquired in the same order. Thread 1 could acquire lock A and thread 2 could acquire lock B and they would deadlock.

    var a = new object();
    var b = new object();
    
    lock(a) {
       lock(b) {
    
       }
    }
    
    // other thread
    lock (b) { 
      lock(a) {
    
      }
    }
    

    edit: non-lock example .. using waithandles. Suppose Socrates and Descartes are having steaks and they both, being well-mannered philosophers, require both a fork and a knife in order to eat. However, they have only one set of silverware, so it is possible for each to grab one utensil and then wait forever for the other to hand over their utensil.

    See the Dining Philosopher's Problem

    WaitHandle fork = new AutoResetEvent(), knife = new AutoResetEvent();
    
    while(Socrates.IsHungry) {
       fork.WaitOne();
       knife.WaitOne();
       Eat();
       fork.Set();
       knife.Set();
    } 
    
    // other thread
    while(Descartes.IsHungry) {
       knife.WaitOne();
       fork.WaitOne();
       Eat();
       knife.Set();
       fork.Set();
    } 
    

提交回复
热议问题