tags:

views:

265

answers:

4

In which situations we can do the multithreading and deadlock concepts. can give some examples

A: 

Your question is too vague... for .NET multithreading, I think this is the best thing to read http://www.albahari.com/threading/

Igor Brejc
+2  A: 

Multi-threading is basically the use of threads, which are certain entities which have access to the same memory space in your process, to run different parts of your process concurrently.

However, concurrent programming is hard, complicated, and dangerous. Deadlock is one of the dangers which arise from that, which means that two threads are waiting on each other to finish a task, ending up in a lock. Suppose you are waiting for me to give you $5 before you give me $5, and I am doing the same. We're both waiting for each other, thus nothing is going to happen.

Like I said, this is a very complicated subject, definitely not something which can be taught on one answer here. I suggest you do some research and find useful resources which can help you grasp the concepts of concurrent programming more firmly.

Yuval A
The $5 example can be a bit confusing, as no money needs to be exchanged at all. The dining philosophers example is a bit better. Or say that person A has a hammer, and person B has the nails, both persons need the hammer and nails at the same time in order to work.
dalle
+1  A: 

Asuming you're asking how deadlocks can occur in a multi-threading environment.

Let's say we have two threads and two resources and the time-line goes something like this:

+===+============================+============================+
| # | Thread 1                   | Thread 2                   |
+===+============================+============================+
| 1 | Allocate resource A, okay. |                            |
+---+----------------------------+----------------------------+
| 2 |                            | Allocate resource B, okay. |
+---+----------------------------+----------------------------+
| 3 | Allocate resource B, lock. |                            |
+---+----------------------------+----------------------------+
| 4 |                            | Allocate resource A, lock. |
+===+============================+============================+

Now you have two threads locked, each waiting for a resource that will never be released, hence deadlock.

The reason the resources cannot be released is because the only threads that can release them are locked. This is why it's a good idea to always have your threads allocate resources in the same order as each other.

paxdiablo
+1  A: 

You would usually get the advice to use the BackgroundWorker component to implement threading. Here's an example of causing it to deadlock:

private void button1_Click(object sender, EventArgs e) {
  backgroundWorker1.RunWorkerAsync();
  // Do something while the BGW runs
  //...
  System.Threading.Thread.Sleep(500);
  // Wait for the result to be available
  while (backgroundWorker1.IsBusy)
    System.Threading.Thread.Sleep(10);
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
  // Do some work
  //...
  System.Threading.Thread.Sleep(1000);  // Pretend to do some work
  e.Result = 42;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
  // Store the result...
}

The deadlock occurs because the BGW has to invoke the RunWorkerCompleted event before it finishes. That cannot happen until the UI thread goes idle. Which doesn't happen here, it is stuck in the loop waiting for the BGW to complete.

Just an example, there are endless ways to get into trouble like this.

Hans Passant