线程安全,异步信号安全之间的区别 [英] Difference between thread safe and async-signal safe

查看:256
本文介绍了线程安全,异步信号安全之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据APUE 2E章12.5:

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, however, whether the function is reentrant with respect to signal handlers. We say that a function that is safe to be reentered from an asynchronous signal handler is async-signal safe.

我的问题是

Q1:

是否有一个总重入的概念(这意味着重新entrantcy在所有情况下)?如果有,做一般折返等于重入相对于两个多线程和仅异步信号?或者是有也有谈论一般重新进入时需要考虑?第三个条件

Is there a "general re-entrant" concept (which means re-entrantcy in all circumstances)? If there is, does general re-entrant equal to re-entrant with respect to both multi-thread and async-signal only? Or is there also a third condition that has to be considered when talking about general re-entrant?

Q2:

主题安全并不意味着异步信号安全性,这是显而易见的。但是,确实异步信号安全意味着线程安全的肯定?我GOOGLE了很多,人们说,这样做,但我找不到任何它的源代码。

Thread safety doesn't imply async-signal safety, which is obvious. But, does async-signal safety imply thread safety for sure? I googled a lot, people are saying that it does, but I can't find any source for it.

Q3:

如果答案Q1和Q2是肯定的,我想一般折返只是等于异步信号安全吗?

If answers to both Q1 and Q2 are yes, I guess general re-entrant just equals to async-signal safe?

推荐答案

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.

这篇关于线程安全,异步信号安全之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆