Questions tagged [deadlock]

Situation where two (or more) operations need overlapping sets of resources, and neither can complete because they cannot obtain all locks necessary to complete an operation and release their locks.

Deadlock is situation where multiple operations are waiting for same resource(s) while simultananeously holding other resources that other threads holding the desired resources are waiting for.

For example, given resources A,B,C and processes 1,2.

  1. 1 locks A and B
  2. 2 locks C
  3. 2 needs A to finish
  4. 1 needs C to finish.

Neither 1 or 2 can release resources before finishing. Therefore, deadlock occurred.

The classical solution to all deadlock problems is to always acquire the resources in the same order in all threads or processes.


Deadlock in Wikipedia

3451 questions
7 answers

What's the difference between deadlock and livelock?

Can somebody please explain with examples (of code) what is the difference between deadlock and livelock?
  • 4,303
  • 4
  • 18
  • 9
9 answers

How to avoid MySQL 'Deadlock found when trying to get lock; try restarting transaction'

I have a innoDB table which records online users. It gets updated on every page refresh by a user to keep track of which pages they are on and their last access date to the site. I then have a cron that runs every 15 minutes to DELETE old records. I…
  • 16,246
  • 34
  • 103
  • 162
3 answers

await vs Task.Wait - Deadlock?

I don't quite understand the difference between Task.Wait and await. I have something similar to the following functions in a ASP.NET WebAPI service: public class TestController : ApiController { public static async Task Foo() { …
  • 49,529
  • 25
  • 126
  • 221
8 answers

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also…
  • 125,244
  • 33
  • 244
  • 253
18 answers

What is a deadlock?

When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My questions to the community are: What is a deadlock? How do you detect them? Do you handle them? And finally, how do you prevent them from…
  • 29,564
  • 11
  • 33
  • 24
6 answers

'await' works, but calling task.Result hangs/deadlocks

I have the following four tests and the last one hangs when I run it. Why does this happen: [Test] public void CheckOnceResultTest() { Assert.IsTrue(CheckStatus().Result); } [Test] public async void CheckOnceAwaitTest() { …
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
6 answers

C++ terminate called without an active exception

I am getting a C++ error with threading: terminate called without an active exception Aborted Here is the code: #include #include #include #include template class…
  • 15,686
  • 6
  • 47
  • 62
4 answers

Re-entrant locks in C#

Will the following code result in a deadlock using C# on .NET? class MyClass { private object lockObj = new object(); public void Foo() { lock(lockObj) { Bar(); } } public void Bar() …
  • 65,082
  • 97
  • 254
  • 325
5 answers

An async/await example that causes a deadlock

I came across some best practices for asynchronous programming using c#'s async/await keywords (I'm new to c# 5.0). One of the advices given was the following: Stability: Know your synchronization contexts ... Some synchronization contexts are…
  • 3,759
  • 6
  • 31
  • 45
4 answers

Cause of a process being a deadlock victim

I have a process with a Select which takes a long time to finish, on the order of 5 to 10 minutes. I am currently not using NOLOCK as a hint to the MS SQL database engine.At the same time we have another process doing updates and inserts into the…
  • 5,523
  • 10
  • 48
  • 87
9 answers

How to implement a lock in JavaScript

How could something equivalent to lock in C# be implemented in JavaScript? So, to explain what I'm thinking a simple use case is: User clicks button B. B raises an onclick event. If B is in event-state the event waits for B to be in ready-state…
  • 41,281
  • 29
  • 127
  • 212
3 answers

How to catch SqlException caused by deadlock?

From a .NET 3.5 / C# app, I would like to catch SqlException but only if it is caused by deadlocks on a SQL Server 2008 instance. Typical error message is Transaction (Process ID 58) was deadlocked on lock resources with another process and has…
Joannes Vermorel
  • 8,976
  • 12
  • 64
  • 104
28 answers

Simple Deadlock Examples

I would like to explain threading deadlocks to newbies. I have seen many examples for deadlocks in the past, some using code and some using illustrations (like the famous 4 cars). There are also classic easily-deadlocked problems like The Dining…
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
3 answers

Why does parallel stream with lambda in static initializer cause a deadlock?

I came across a strange situation where using a parallel stream with a lambda in a static initializer takes seemingly forever with no CPU utilization. Here's the code: class Deadlock { static { IntStream.range(0, 10000).parallel().map(i…
Reinstate Monica
  • 2,420
  • 14
  • 23
13 answers

Write a program that will surely go into deadlock

I recently got this questions asked in an interview. I answered that deadlock occurs if the interleaving goes wrong, but the interviewer insisted that a program that will always go into deadlock regardless of interleaving can be written . Can we…
  • 6,339
  • 18
  • 63
  • 87
2 3
99 100