Access shared data from a signal handler

I want to know if accessing shared data from a signal handler is a good idea. I am referring to a scenario of a multiprocessor system and a multi-threaded system with one process. In a multiprocessor system, it can be said that processes process a specific signal and update certain common variables or memory by processes. Can I do this from the signal handler itself.

However, in the case of threads using pthreads, I do not think this is doable. http://maxim.int.ru/bookshelf/PthreadsProgram/htm/r_40.html. As indicated in this article, they mentioned that it is not a safe asynchronous signal and suggested using a sigwait for this. I am not because it is not a safe asynchronous signal. I mean, say, I process the signal with a stream and gets into the routing of the signal handler. I get a lock in shared memory to update it. At the same time, another signal of the same type appears, and another thread responsible for processing it again executes the signal handler. Here, the signal handler is the same for the process, but it is called multiple times. The second time, he does not see the lock and updates / cancels the data. This is a problem with multi-threaded signal handlers that use common data.

I'm a little confused, in multiprocessor systems I have a copy of the signal handler for each process. But in a multi-threaded system, there is one copy of the signal handler used by several threads, not it. Therefore, when several signals of the same type arrive, and we have two threads that are responsible for its processing, try to process it, then both of them will try to execute the same code of the handler code? How does all this fit in?

+5
source share
1 answer

, "Threads in Signal Handlers" . , Posix, . , , :

Pthreads? ! , Pthreads , Pthreads undefined, . , - , - . Pthreads .

: " Pthreads "

, , :

, , .

pthread , reentrancy, , (, pthread_mutex_lock()) , "".

, pthread_mutex_lock(&theMutex) ( pthread_mutex_lock()) . pthread_mutex_lock(&theMutex), pthread , , pthread_mutex_lock() . , undefined/​​.

, sigwait() , non-reentrancy , "" pthread.

+4

All Articles