重入与线程安全

在stackoverflow上看的的关于可重入与线程安全的区别:These functions are not reentrant. They are thread-safe. There's a big difference. Functions that are reentrant are automatically thread-safe, but thread-safe functions can still deadlock (or worse) if called again from the same thread they're already running in (for example from a signal handler).安全

函数可重入很天然会以为该函数是线程安全的,既然能被其余流程中断并可调用自身也就能够被多个线程同时调用。可是线程安全是否是可重入有点模糊,上面说了一种状况,当重复调用一个线程安全的函数,而且函数中有锁就有可能发生死锁,代表线程安全不必定可重入。看来重入的要求比线程安全要更严格一点。函数

附带查了查关于Posix的标准io操做,Posix标准要求操做字符流的函数实现为可重入,可是各个实现为了性能方面的考虑都没有实现为可重入的,这些函数有潜在不安全。可是又说明:all standard I/O functions that reference character streams shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of the character streams,代表它们应该是线程安全的。性能

相关文章
相关标签/搜索