线程安全 – 这些是线程安全的吗?

我今天参加了面试,面试官问我以下问题:

重新入侵和互斥是线程安全的吗?你能解释一下原因吗?

我对并发编程比较陌生,无法回答它..但我说……

相互排斥是线程安全的.但重新入门不是,这就是为什么我们有可重入锁定的原因.

面试官转到下一个问题但是到了另一个地方……我想我搞砸了这个……

当他问我这个时,他期待我说什么?

最佳答案
适当的答案应该是:

是的,他们是线程安全的实现.

重入

Writing code in such a way that it can be partially executed by one task, reentered by another task, and then resumed from the original task. This requires the saving of state information in variables local to each task, usually on its stack, instead of in static or global variables.

one example

相互排斥

Access to shared data is serialized using mechanisms that ensure only one thread reads or writes the shared data at any time. Great care is required if a piece of code accesses multiple shared pieces of data—problems include race conditions, deadlocks, livelocks, starvation, and various other ills enumerated in many operating systems textbooks.

one example

转载注明原文:线程安全 – 这些是线程安全的吗? - 代码日志