Pthread_exit () in a signal handler

(This question may be somewhat related to pthread_exit in the signal handler causing a segmentation error ) I am writing a leadlock prevention library, where there is always control thread graphic material and checks if there is a dead end, if so, it signals one of the conflicting threads. When this thread captures a signal, it releases all the mutex (s) that it owns and exits. There are several resource mutexes (obviously) and one critical area mutez, all calls to receive, release resource locks and schedule calculations must first obtain this lock. Now the problem. When using 2 competing (not counting verification threads), sometimesprogram deadlocks after one thread is killed. Gdb says that a dead branch has a lock on a critical region, but never released it. After adding a breakpoint in the signal handler and passing through, it seems that the lock belongs to someone else (as expected) right before pthread_exit (), but possession magically passes into this thread after pthread_exit () ..The only guess I can think of is that the thread that needs to be killed blocks pthread_mutex_lock when trying to get a critical region lock (because it needs a different resource mutex), then the signal came in, interrupting pthread_mutex_lock. Since this call is not a signal, did something strange happen? How could the signal handler come back and this thread got a lock and then exited? Idk .. Any insight appreciated!

+1
source share
1 answer

pthread_exit is not safe with respect to an asynchronous signal, and thus the only way to call it from a signal handler is to make sure that the signal does not interrupt any function without an asynchronous signal.

, . , : ( ) .

, , pthread_cancel. , , , , / , ( pthread_setcancelstate). , , pthread_mutex_lock . , , , , , , , , ( condvar ), .

: , , pthread_mutex_lock , pthread_mutex_timedlock .

+3

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


All Articles