Only one thread can cause a deadlock or program freeze on Linux?

I am doing multithreaded C ++ programming. I use the mutex to read and write the queue to avoid a deadlock. Currently I only run 1 thread for

pthread_mutex_lock(&the_mutex); 

But in GDB, my code is frozen here, it is pending.

Why? there is only one thread !!!

thanks

+4
source share
1 answer

On the pthread_mutex_lock() page:

If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection should not be provided. Attempting to lock the mutex causes a deadlock. If thread tries to unlock a mutex that it hasn't locked, or a mutex that is unlocked, undefined behavior results.

If the mutex type is PTHREAD_MUTEX_DEFAULT, try recursively blocking the mutex results in undefined mode. Attempting to unlock the mutex if it was not blocked by the calling thread results in undefined behavior. Attempt to unlock the mutex if it is not locked. in undefined mode.

Bottom line: It is possible to cause a deadlock with only one thread if you try to block an existing mutex.

In case you are interested, Linux PTHREAD_MUTEX_DEFAULT usually synonymous with PTHREAD_MUTEX_NORMAL , which in turn is what is used by default in the mutex initializer.

+6
source

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


All Articles