The difference between thread safety and asynchronous signal security

According to APUE 2e Chapter 12.5:

If a function is reentrant with respect to multiple threads, we say that it is thread safe. However, this does not tell us whether the function is reentrant with respect to signal handlers. We say that a function that is safe for re-entry from an asynchronous signal handler is safe for an asynchronous signal.

My questions

1:

Is there a concept of “shared re-participation” (which means re-participation in all circumstances)? If so, is the common re-entry equal to the repeater only for both multi-threaded and asynchronous signals? Or is there also a third condition that needs to be considered when talking about general re-participation?

2:

Thread safety does not imply asynchronous signal security, which is obvious. But does asynchronous signal safety imply thread safety? I google a lot, people say that it’s so, but I can’t find any source for this.

3:

If the answers to Q1 and Q2 are yes, I assume that the common re-entry is simply equal to the safe signal of the asynchronous signal?

+6
source share
1 answer

Q1: A safe signal with an asynchronous signal is the strongest reentrant concept. This requires very careful use of resources and is difficult to manage in a cross-platform application code.

Q2: Safe asynchronous signal implies thread safety. A thread safe means that you need to try calling the function twice, but from different threads; asynchronous signal security is stronger because two function calls can be on the same thread. This complicates the work because you cannot just wait until another function call releases its locks, the second call inside the signal handler should be able to interrupt the first, even if the shared resources are in an inconsistent state, and then restore things when they exit. In principle, it is not possible to use shared resources / state from a signal handler: always use a "trick with a self-recording" if you really do not know how the signal handlers work, and there is some unclear reason for writing crazy code.

Q3: Some people may use reentrant to keep in mind only thread safety. Unix signal handlers are the only place where something more powerful is needed, and this is a bit unclear because you should not try to do anything smart.

+5
source

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


All Articles