对于单处理器,锁定算法非常简单。
Lock(threadID) {
Disable Interrupts
If lock is already owned by same thread{
Restore Interrupts
return
}
if lock is free {
make lock busy
set current thread as the owner of the lock
}
else {
add threadID to the lock queue.
}
Restore Interrupts
return
}
但是我们如何在多处理器/多核系统中实现此代码。如果 2 个核心/进程尝试为不同的进程提供相同的锁怎么办。