I have a function which is intermittently throwing the error
__pthread_mutex_lock: Assertion `mutex->__data-__owner == 0' failed.
From my research till now I came to know the following cases in which this deadlock happens (please correct me if I am wrong):
- If a thread has locked a mutex and again trying to lock the same mutex.
- If the mutex the thread is trying to lock is either not initialized or destroyed/corrupted.
- Corruption of memory stack.
The function has several sections using mutexes but a senior of mine pointed out a particular section and asked me to look into it, though even he couldn't pinpoint the problem but I am trusting his intuition. This part is a for loop which is repeatedly calling a send() function which is writing data to a message queue. Though from my review of the code till now I couldn't find any way in which the code might hit case 1 or 2 all mutexes are properly locked and released. Which leads me to suspect case 3, but the problem is that case 3 is very rare, but the occurrence of the issue is not that rare. Hence I am requesting anyone with a better understanding to take look into the code and confirm whether it is safe to call the send() function over in a loop, if yes than what might be causing mutex lock error. Here is the pseudo code:
class SafeMutex{
pthread_mutex_t s_mutex;
public:
void lock(){
pthread_mutex_lock(&s_mutex);
}
void unlock(){
pthread_mutex_unlock(&s_mutex);
}};
SafeMutex mymutex;
int send(){
mymutex.lock();
if(Condition A){
}
else{
if(Condition B){
mymutex.unlock();
return 1;
}
}
mymutex.unlock();
if(Condition C){
mymutex.lock();
if(Condition B)
{ mymutex.unlock();
return 1;
}
mymutex.unlock();
if(Condition C)
{ return 1;
}
}
return 0;
}
void SendMessages() {
for (i = 0; i < 30; i++) { //this loop is calling send() with proper argument
send();
}
}
PS: I am a noob still learning, please be easy on me :).
There is this question where the author has talked about lower level race condition in pthread_mutex_lock file, If anyone can tell further about this then it will be great: Pthread mutex lock assertion fails