Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_mutex_lock locks, but no owner is set

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 986
dbeer Avatar asked Jul 14 '11 16:07

dbeer


People also ask

What happens if a mutex is already locked?

Mutexes are used to protect shared resources. If the mutex is already locked by another thread, the thread waits for the mutex to become available. The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it.

Can pthread_mutex_lock fail?

The pthread_mutex_lock() function may fail if: [EDEADLK] A deadlock condition was detected or the current thread already owns the mutex.

What is the purpose of pthread_mutex_lock () function?

The pthread_mutex_lock() function acquires ownership of the mutex specified. If the mutex currently is locked by another thread, the call to pthread_mutex_lock() blocks until that thread relinquishes ownership by a call to pthread_mutex_unlock().

Does pthread_mutex_lock use any system calls?

An instance of a pthread_mutex_t should be defined globally and initialized when it is declared with the macro PTHREAD_MUTEX_INITIALIZER. There are three system calls which perform operations on a mutex, each of which takes a pointer to a pthread_mutex_t as an argument. int pthread_mutex_lock(pthread_mutex_t *mutex);


1 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 168
R.. GitHub STOP HELPING ICE Avatar answered Oct 03 '22 06:10

R.. GitHub STOP HELPING ICE