我有以下代码:
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
那么如何在系统中实现锁定?将变量放在另一个变量之上是不对的:这就像守卫?
停止其他处理器线程也不对吗?