0
votes

How to name a situation when a thread is locked waiting forever? I found several examples about when two threads collide, which is called a race condition:

A race condition occurs when two or more threads 
can access shared data and they try to 
change it at the same time

But when a thread is stuck waiting forever for another thread, is there any specific term to name this situation, or yet is it still named as a race condition, even if we have two thread that do not try to access the shared data at the same time, but instead... one of the threads got locked waiting forever for the 2nd thread?

1
It's called Deadlock.Tatsuyuki Ishi
..or possibly livelock, if the thread/s are at 100% CPU.ThingyWotsit
It depends on why the thread is waiting. If thread 1 is using a resource, and then starts waiting for another to be free, but that other resource won't ever be free because it was claimed by thread 2, which is now waiting for the resource thread 1 has, that's deadlock (and there are several versions of the same thing, but they're all roughly the same idea). If one of the threads is waiting on the other, and the other is just never ending for some other reason (e.g. infinite loop) that's just... an infinite loop.Nic

1 Answers

0
votes

If Thread_1 is waiting for Thread_2, and at the same time, Thread_2 is waiting for Thread_1. It's called dead-lock. When Dead-Lock occurred, it just like two threads tussle with each other.

Another situation is called live-lock. Live-lock is just the situation that there is a thread waiting forever for another thread. If Thread_2 is waiting for Thread_1. And Thread_2 want to get a write-lock from Thread_1. But at this time there comes another Thread_3. Thread_3 wants to get a write-lock from Thread_1, too. Unfortunately Thread_3 got a higher priority than Thread_2. So When Thread_1 free that write-lock. Thread_3 got that write-lock immediately. And there comes a Thread_4, Thread_4 has the same priority as Thread_3...

Thread_2 seems to be waiting for a write-lock forever. That's called a live-lock. To avoid this problem, we can build a server-queue. Which thread came to queue early, we handle that earlier thread first.

Pthread gave us serval ways to lock a resource. We can use mutex or something other. But we must solve the issue after we using locks.

You could get more information about this topic in Concurrency Control.