0

I guess I got problems with deadlock...My program is working well but some time it get unresponsive...???. In source code (C#)..have no lock(objet) or ReaderWriterLockSlim... I did try to reproduce the deadlock with 2 threads using same object (List) but no success..

int n = 0;                      
  List<int> temp = new List<int>();
  var up = new Thread(() =>{
     for (int i = 0; i < 100000; i++){
     n++;
     temp.Add(i);                   
}
});

var down = new Thread(() => {
    for (int i = 0; i < 100000; i++){
        n--;
         try{
           temp.Remove(i);
         }catch {
           Console.WriteLine("No item {0} to remove", i);
         }
        }
});

up.Start();
down.Start();
down.Join();
up.Join();
Console.WriteLine(String.Join(",",temp));

The snipe code above still works without deadlock..??? Could someone help me to reproduce the deadlock with 2 threads using same variable without lock(object) or System lock...

Thank in Advance

hoanganh17b
  • 867
  • 1
  • 11
  • 25
  • 3
    What code _does_ have the problem? Are you sure it's a threading deadlock, and not something else causing unresponsiveness? – Tim S. Sep 27 '13 at 17:37
  • 2
    Can you reproduce the problem with your production code? – Sriram Sakthivel Sep 27 '13 at 17:42
  • 2
    Since you have no lock in this code, so there can not be a deadlock problem, but weird behavior. – VahiD Sep 27 '13 at 17:43
  • http://stackoverflow.com/questions/8064296/is-deadlock-possible-when-locking-one-global-object-in-asp-net-mvc-application – L.B Sep 27 '13 at 17:44
  • when its stuck, break in with the debugger and see where the threads are – pm100 Sep 27 '13 at 17:47
  • Use WinDbg and attached to the process when it is hung to figure out why. Also, the code you posted isn't thread-safe. The `Add` or `Remove` calls might throw an exception, corrupt the data structure, or even tear a whole in spacetime. Either way eventually something spectacular will occur. – Brian Gideon Sep 27 '13 at 18:08
  • Deal all. Actually I want to reproduce a deadlock to get understanding how it happens. Because my program using opensource framework is written in multithreading pattern so I guess problem come from threads are using same resource... I did try snipe code above with and expect that may cause a deadlocking but nothing happen.. So I need someone help me to reproduce this problem without lock or system lock.. @Brian Gideon..thank for your suggestion..Actually the snipe code still working well so I have no chance to see in WinDbg – hoanganh17b Sep 27 '13 at 18:33
  • @Sriram Sakthivel. No, I can't reproduce the problem. Sometime system hang up (1 or 2 times perday). – hoanganh17b Sep 27 '13 at 18:44
  • @Tim S I guess..., because other part of code is very simple and I don't believe it may cause problem. – hoanganh17b Sep 27 '13 at 18:46
  • I'll suggest you to try simulating the problem with your program in debug mode. If simulated then pause the debugger and open Debug->Windows->Threads to inspect all thread's execution point you can easily find where main thread is blocked on which resource and which thread is causing the deadlock to happen. – Sriram Sakthivel Sep 27 '13 at 18:59

2 Answers2

3

List is not thread-safe so this will be the cause of your problem. You should create a thread-safe solution (using lock or with thread-safe collections) and not focus on recreating the deadlock. What will that tell you?

meilke
  • 3,280
  • 1
  • 15
  • 31
  • Thank @meilke..I guess problem come from non thread-safe-mode. But I need to reproduce the system hang before apply any solution..And I what I need now is looking for snipe code can let system hang up. (without lock object or system lock). – hoanganh17b Sep 27 '13 at 18:38
  • @hoanganh17b, I agree, it is important to reproduce problems so that when we do fix issues, we are confident that we are addressing the actual problem. I found this blog post interesting: http://blogs.msdn.com/b/tess/archive/2009/12/21/high-cpu-in-net-app-using-a-static-generic-dictionary.aspx – Pooven Aug 23 '15 at 19:46
1

Normally a deadlock occurs when using locks or synchronization.

Most common deadlocks are updateing the Gui from a thread. Like:

Thread thread = new Thread( () => 
{ 
    Invoke(new Action(() => 
    {
        Label1.Text = "something";
    }));
});

thread.Start();

// this join will prevent the messageloop from running, but the invoke waits on the messageloop to execute it's action.
thread.Join();

But it isn't like your case.

I think you mean the n is increased and decreased (same as mutating the list) on different threads. This isn't a deadlock, but threadsafety problem.

Even if you put a lock into it, you don't get a deadlock there.

object lockObj = new object();

 var up = new Thread(() =>
 {
     for (int i = 0; i < 100000; i++)
     {
         lock(lockObj)
         {
             n++;
             temp.Add(i);                   
         }
     }
 });

 var down = new Thread(() => 
 {
     for (int i = 0; i < 100000; i++)
     {
         lock(lockObj)
         {
             n--;
             try
             {
                 temp.Remove(0);
             }
             catch 
             {
                 Console.WriteLine("No item {0} to remove", i);
             }
         }
     }
});

But this will slowdown the process very much.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • ...thank to much for your comment. But without lock(lockObj)..code is still working well and I want to understand why..And how to reproduce system hang up with thread without lock.. – hoanganh17b Sep 27 '13 at 18:40