1

I have some code with many functions and threads and many different mutexes. Some times the code works well and some times I get one of these errors

1) "/usr/include/boost/thread/pthread/mutex.hpp:111: boost::mutex::~mutex(): Assertion '!res' failed."

2) "/usr/include/boost/thread/pthread/condition_variable.hpp:168: boost::condition_variable_any::~condition_variable_any(): Assertion '!pthread_mutex_destroy(&internal_mutex)' failed."

I thought maybe one of the mutexes is getting destroyed before it is unlocked. So, I investigated all the pthread_mutex_destroys in the destructors, but can't find any problem.

For example one of the destructors is as bellow

~class1(){ 
// unlock threads waiting at push or pop
pthread_mutex_lock(&mutex);
pthread_cond_signal(cv1);
pthread_cond_signal(cv2);
pthread_mutex_unlock(&mutex);

// wait for threads blocked in push or pop to exit
while(number_of_threads>0){
usleep(100);
}

//wait them to exit and destroy cv and mutex 
pthread_mutex_lock(&mutex);
pthread_cond_destroy(&cv1);
pthread_cond_destroy(&cv2);
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
}

The number_of_threads increases and decreases in the push and pop functions which use the mutex and the condition variables.

How can I trace the code to find out where and why the problem occurs. I have many mutexes and functions so it is not an easy job to find the problem. Any hints will help.

Thanks in advance

zahra
  • 175
  • 1
  • 10
  • 3
    Please provide a [mre]. We cannot imagine your code. – L. F. Jun 10 '19 at 11:23
  • 2
    First go through crashing thread call stack, then confront it with call stacks of other threads. Looks like you incorrectly used `condition_variable`. – Marek R Jun 10 '19 at 11:30
  • If you use debugger you use trace stack to the mutex. You will probably have to use `info threads` and `thread N` command. – Anže Jun 10 '19 at 11:30
  • 3
    @L.F. That generic comment is useless here. He's asking specifically how to trace the code to locate the concurrency issue. We don't need his code in order to help him with that. – simon Jun 10 '19 at 11:34

0 Answers0