重入与线程安全

在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

表明它们应该是线程安全的。

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2015-2024 RivenZoo
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信