重入與線程安全

在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,代表它們應該是線程安全的。性能

相關文章
相關標籤/搜索