multithreading - How to determine when threads are in a deadlock from Main C# -
i have read other posts tools deadlock detection , avoiding deadlock; question how find when threads in deadlock main can make main thread sleep until threads deadlocked , proceed further.
i tried use threadstate.waitsleepjoin detect whether threads deadlocked longer period of time in main, doesn't seem working; there utility methods available determine when threads deadlocked?
i trying implement deadlock scenario in dining philosophers when threads take either right or left chopstick first, wait other chopstick forever , hence deadlock occurs; issue not able find when these threads enter waiting state ever main.
this 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 main:
public static list<diningstate> mainfunction(int numberphilosophers) { thread[] phil = new thread[numberphilosophers]; chopstick[] fork = new chopstick[numberphilosophers]; deadlockdiningphilosophercontroller ph = null; (int = 0; < numberphilosophers; ++i) { fork[i] = new chopstick(i); } stopwatch time = stopwatch.startnew(); (int = 0; < numberphilosophers; ++i) { ph = new deadlockdiningphilosophercontroller(i, fork[(i - 1 + numberphilosophers) % numberphilosophers], fork[i]); phil[i] = new thread(new threadstart(ph.run)); phil[i].start(); } (int = 0; < numberphilosophers; i++) { if (phil[i].threadstate != system.threading.threadstate.waitsleepjoin) { = - 1; thread.sleep(10000); } else { continue; } } (int = 0; < 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 replace waitsleepjoin efficient solution find out when threads deadlocked?
please help, suggestions appreciated! thank you!
if thread deadlocked, don't think can make work main thrad. thing can trying avoid deadlock using lock, mutex, monitor, etc.
Comments
Post a Comment