问题描述
在APUP第12.5节中,提到:
In APUP section 12.5 ,mentioned that:
"如果一个函数相对于多个线程是可重入的,那么我们说它是线程安全的.但是,这并没有告诉我们该函数相对于信号处理程序是否可重入."
"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."
我感到困惑的是,为什么一个函数是线程安全的,但可能不是异步信号安全的.
I was confusing why a function is thread-safe but maybe not async-signal safe.
有例子吗?
谢谢
推荐答案
通过使用互斥锁保护函数修改的状态,可以使该函数成为线程安全的.但是,这不是异步信号安全的,因为如果您调用此函数,例如从信号处理程序中,程序可能会死锁.
A function can be made thread-safe by protecting the state it modifies with a mutex. This is, however, not async-signal-safe since if you call the function e.g. from a signal handler the program can deadlock.
具有此属性的常见函数是malloc().
A common function with this property is malloc().
这篇关于线程安全与异步信号安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!