Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_mutex_lock returns invalid argument

I am working on some C code and am having a problem with locking a mutex. The code does a call to a function and this function locks a mutex to ensure a file pointer doesn't get overwritten, this works fine for several instances, probably about 10-20 separate calls of the function being called, but on the next call, pthread_mutex_lock will return with a result of 22. I've then put this result into strerror(); and got back invalid argument.

What does invalid argument means, thanks for any help you can provide.

like image 532
Boardy Avatar asked Oct 08 '12 12:10

Boardy


2 Answers

22 is ENVAL error code which means invlalid argument. Make sure that you have initilized you mutex, or if at some point you have unitilized it somewhere.

Also man pthread_mutex_lock says:

EINVAL

The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling.

I don't quite understand this but it probably means that you need to change thread's priority. I am not sure. Maybe someone else can shine light on it.

like image 120
tozka Avatar answered Oct 17 '22 18:10

tozka


Sounds like you have a threading problem or a wild point somewhere else in your program. Try printing the value of the mutex pointer. Try having another thread that simply locks the mutex and then prints to a log file the time and that the lock was successful, then unlocks the mutex. I suspect the problem is not where you are looking.

Also, as other have said here, your best bet is to create a very small test program that demonstrates the problem and post it here. Chances are you won't be able to get that small program to demonstrate the error. Then slowly add all of your original code into the small program until the error returns. If it returns, you now know what caused the problem. If it doesn't return, you're done.

like image 30
vy32 Avatar answered Oct 17 '22 17:10

vy32