Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do deadlocks cause high CPU utilization?

Do deadlocks put processes into a high rate of CPU usage, or do these two processes both "sleep", waiting on the other to finish?

I am trying to debug a multithreaded program written in C++ on a Linux system. I have noticed excessive CPU utilization from one particular process, and am wondering if it could be due to a deadlock issue. I have identified that one process consistently uses more of the CPU than I would anticipate (using top), and the process works, but it works slowly. If deadlocks cause the processes to sleep and do not cause high CPU usage, then at least I know this is not a deadlocking issue.

like image 776
Jonathan Avatar asked Nov 28 '22 01:11

Jonathan


2 Answers

A deadlock typically does not cause high CPU usage, at least not if the deadlock occurs in synchronization primitives that are backed by the OS such that processes sleep while they wait.

If the deadlock occurs with i.e. lockless synchronization mechanisms (such as compare-exchange with an idle loop), CPU usage will be up.

Also, there is the notion of a livelock, which occurs when a program with multiple threads is unable to advance to some intended state because some condition (that depends on interaction between threads) cannot be fulfilled, even though none of the threads is explicitly waiting for something.

like image 98
Alexander Gessler Avatar answered Dec 05 '22 07:12

Alexander Gessler


It depends on the type of lock. A lock that is implemented as a spin loop could run up 100% CPU usage in a deadlock situation.

On the other hand, a signalling lock such as a kernel mutex does not consume CPU cycles while waiting, so a deadlock on such a lock would not peg the CPU at 100%

like image 39
dthorpe Avatar answered Dec 05 '22 09:12

dthorpe