Difference between thread safe and async-signal safe

前端 未结 1 1893
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 21:27

According to APUE 2e Chapter 12.5:

If a function is reentrant with respect to multiple threads, we say that it is thread-safe. This doesn\'t tell us, howe

1条回答
  •  执念已碎
    2021-02-05 21:46

    Q1: async-signal safe is the strongest concept of reentrancy. It requires very careful use of resources and is hard to manage in cross-platform application code.

    Q2: async-signal safe implies thread safe. Thread safe means it's OK to try calling the function twice, but from different threads; async-signal safe is stronger because two invocations of the function can be in the same thread. This makes things much harder, because you can't simply wait for the other invocation of the function to release its locks, the second call inside the signal handler has to be able to interrupt the first one even if shared resources are in an inconsistent state, then restore things when it exits. It's basically impossible to use shared resources/state from a signal handler: always use the "self-pipe trick" unless you really know how signal handlers work and have some obscure reason for writing insane code.

    Q3: some people may use reentrant to mean just thread safe. Unix signal handlers are the only common place where anything stronger is needed, and that's a bit obscure because you shouldn't be trying to do anything clever there.

    0 讨论(0)
提交回复
热议问题