Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When several threads are blocked on a lock, in what order do they resume running?

Threads A, B, C in that order, reach synchronized method f() in a single object.

All have the same priority.

B and C are blocked.

A leaves f().

What thread now begins running in f()? Is it always B on the principle of FIFO? Or is the order undetermined?

If C has a higher priority than B, does that guarantee that C will run rather than B?

like image 252
Joshua Fox Avatar asked Jul 29 '10 15:07

Joshua Fox


People also ask

What happens when a thread gets blocked?

The running thread will block when it must wait for some event to occur (response to an IPC request, wait on a mutex, etc.). The blocked thread is removed from the running array, and the highest-priority ready thread that's at the head of its priority's queue is then allowed to run.

What happens when multiple processes threads are trying to modify the same data?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Can multiple threads take a lock simultaneously?

Only one thread can hold a lock at a time. If a thread tries to take a lock that is already held by another thread, then it must wait until the lock is released.

What is blocked multithreading?

Conceptually, it is similar to cooperative multi-tasking used in real-time operating systems, in which tasks voluntarily give up execution time when they need to wait upon some type of the event. This type of multithreading is known as block, cooperative or coarse-grained multithreading.


1 Answers

For all application intents and purposes you can assume the order is totally random. Don't play with priorities - you can easily introduce subtle priority inversion bugs that are very very very hard to catch.

like image 83
Nikolai Fetissov Avatar answered Nov 03 '22 00:11

Nikolai Fetissov