0

I have read other posts about tools for deadlock detection and avoiding deadlock; but my question is specifically about how to find when threads are in deadlock from Main so that I can make the Main thread sleep until the threads are deadlocked and proceed further.

I tried to use ThreadState.WaitSleepJoin to detect whether all threads are deadlocked for a longer period of time in Main, but that doesn't seem like working; Are there any utility methods available to determine when all threads are deadlocked?

I am trying to implement deadlock scenario in Dining philosophers where when all the threads take either right or left chopstick first, they will wait for the other chopstick forever and hence deadlock occurs; only issue is that I am not able to find when all these threads enter into waiting state for ever from Main.

This is my Run method:

public virtual void Run()
    {
        while (!stop)
        {
            Random random = new Random();
            try
            {
                outputList.Add("Thinking", identity);
                Thread.Sleep(random.Next(1, 100000000));

                outputList.Add("Hungry", identity);
                right.get();
                outputList.Add("Got Right", identity);

                Thread.Sleep(random.Next(1, 100000000));

                left.get();
                outputList.Add("Got Left", identity);
                outputList.Add("Eating", identity);

                Thread.Sleep(random.Next(1, 100000000));
                right.put();
                left.put();
            }
            catch (Exception e)
            { }
        }
    }

This is Main:

      public static List<DiningState> MainFunction(int numberPhilosophers)
      {
        Thread[] phil = new Thread[numberPhilosophers];
        ChopStick[] fork = new ChopStick[numberPhilosophers];
        DeadlockDiningPhilosopherController ph = null;

        for (int i = 0; i < numberPhilosophers; ++i)
        {
            fork[i] = new ChopStick(i);
        }

        Stopwatch time = Stopwatch.StartNew();

        for (int i = 0; i < numberPhilosophers; ++i)
        {
            ph = new DeadlockDiningPhilosopherController(i, fork[(i - 1 + numberPhilosophers) % numberPhilosophers], fork[i]);
            phil[i] = new Thread(new ThreadStart(ph.Run));
            phil[i].Start();
        }

        for (int i = 0; i < numberPhilosophers; i++)
        {
            if (phil[i].ThreadState != System.Threading.ThreadState.WaitSleepJoin)
            {
                i = i - 1;
                Thread.Sleep(10000);
            }
            else
            {
                continue;
            }
        }
        for (int i = 0; i < numberPhilosophers; i++)
        {
            if (phil[i].ThreadState == System.Threading.ThreadState.WaitSleepJoin)
            {
                phil[i].Abort();
            }
        }
        time.Stop();

        int timespan = (int)time.Elapsed.TotalSeconds;
        Console.WriteLine("Total Time: " + time.Elapsed.TotalMilliseconds + time.Elapsed.TotalSeconds);

        return outputList;
     }

How can I replace the WaitSleepJoin with an efficient solution to find out when all threads are deadlocked?

Please help, any suggestions are greatly appreciated! thank you!

4

1 回答 1

0

如果一个线程死锁,我认为你不能让它从主线程工作。我们唯一能做的就是尝试使用锁、互斥锁、监视器等来避免死锁。

于 2013-05-21T03:38:56.690 回答