Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplainable pthread mutex deadlock [duplicate]

Tags:

c

pthreads

I've been working on this one for a few days -

As a background, I'm working on taking a single-threaded C program and making it multi-threaded. I have recently discovered a new deadlock case, but when I look at the mutex in gdb I see that

__lock=2 yet __owner=0

This is not a recursive mutex. Has anyone seen this? The program I'm working on is a daemon and this case only happens after executing at a high-throughput rate for over 20 minutes (approximately) and then relaxing the load. If you have any ideas I'd be grateful.

Edit - I neglected to mention that all of my other threads are idle at this time.

Cheers

like image 215
dbeer Avatar asked Apr 29 '26 18:04

dbeer


2 Answers

This is to be expected. A normal (non-recursive, non-errorchecking) mutex has no need to store its owner, and some time can be saved skipping the step of looking up the caller's thread id. (This makes little difference on x86 but can be a huge difference on platforms like MIPS with broken ABIs, where there is no thread register and getting the thread id incurs a fault into kernelspace.)

The deadlock you're seeing it almost certainly due either to the thread trying to lock a mutex it already holds, or an actual logic error where two or more threads are each waiting for mutexes the other holds.

like image 125
R.. GitHub STOP HELPING ICE Avatar answered May 02 '26 07:05

R.. GitHub STOP HELPING ICE


As far as I can tell, this is due to a limitation of the pthread library. Whenever I have found parts of the code that use excessive locking and unlocking and heavily stressed that section of the code, I have had this kind of failure. I have solved them by re-writing these sections to minimize their locking, which is easier code to maintain (less error checking when re-acquiring potentially freed objects) and eliminates some overhead.

like image 31
dbeer Avatar answered May 02 '26 07:05

dbeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!