我有以下代碼:如何實現鎖定?
while(lock)
;
lock = 1;
// critical section
lock = 0;
作爲讀取或改變鎖值本身是一種多指令
read lock
change value
write it
如果它發生像:
1) One thread reads the lock and stops there
2) Another thread reads it and sees it is free; lock it and do something untill half
3) First thread wakes up and goes into CS
SO如何將鎖定會在系統中受到影響嗎? 將變量放在另一個變量的頂部是不對的:它會像守衛守衛?
停止其他處理器線程也是不對的?
對於哪種算法,你有這個代碼? – perilbrain 2012-08-14 11:14:06
「*由於讀取或更改鎖定值本身就是一個多指令*」 - 並非總是如此。原子操作由幾種體系結構提供。鎖實現使用這些,這取決於硬件。 – ArjunShankar 2012-08-14 11:31:40
該操作(指令)的原子性如何實現? – 2012-08-14 11:36:59