且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

线程安全,异步信号安全之间的区别

更新时间:2021-07-10 01:04:55

Q1:异步信号安全是重入最强的概念。它需要非常小心地使用资源,难以在跨平台的应用程序code来管理。

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:异步信号安全意味着线程安全的。线程安全意味着它是确定以重调用该函数的两倍,但在不同的线程;异步信号安全更强,因为函数的两个调用可以在相同的线程。这使事情变得更难,因为你不能简单地等待函数的调用其他释放其锁定,信号处理程序中的第二个呼叫必须能够打断第一个即便共享资源处于不一致的状态,然后还原事情退出时。它基本上是不能使用共享资源/状态从信号处理:始终用自管帽子戏法除非你真的知道信号处理程序是如何工作的,并有写疯狂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:有些人可能会使用折返意味着只是线程安全的。 Unix的信号处理程序是在这里什么都强,需要的唯一共同的地方,那是一个有点模糊,因为你不应该试图做任何高明在那里。

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.