Pthread_mutex_lock returns an invalid argument

I am working on some C code and I have a problem with locking a mutex. The code makes a function call, and this function blocks the mutex to ensure that the file pointer will not be overwritten, this works fine for several instances, maybe about 10-20 separate calls to the called function, but the next time it calls, pthread_mutex_lock will return with result 22. Then I put this result in strerror (); and got the wrong argument.

Which means an invalid argument, thanks for any help you can provide.

+4
source share
2 answers

It looks like you have a threading problem or a wild dot somewhere else in your program. Try typing the value of the mutex pointer. Try another thread that simply locks the mutex and then prints the time to the log file and that the lock is successful and then unlocks the mutex. I suspect that the problem is not what you are looking for.

Also, as others have said, it’s best to create a very small test program that demonstrates the problem and publishes it here. Most likely, you will not be able to get this small program to demonstrate this error. Then slowly add all your source code to the small program until the error returns. If he returns, you now know what caused the problem. If he does not return, you are done.

+4
source

22 is an ENVAL error code that means an invalid argument. Make sure you initialize the mutex, or if you unify it at some point.

Also man pthread_mutex_lock says:

Einval

The mutex was created with a protocol attribute of PTHREAD_PRIO_PROTECT and the priority of the calling thread is higher than the priority ceiling of the current mutex.

I don't quite understand this, but that probably means you need to change the priority of the thread. I'm not sure. Maybe someone else can shine on it.

+7
source

Source: https://habr.com/ru/post/1438406/


All Articles