人类是 sequential creature
人类是 (不轻言放弃的) sequential creature
插入 “神秘代码”,使得所有其他 “神秘代码” 都不能并发
void Tsum() {
stop_the_world();
// 临界区 critical section
sum++;
resume_the_world();
}
Stop the world 真的是可能的
int locked = UNLOCK;
void critical_section() {
retry:
if (locked != UNLOCK) {
goto retry;
}
locked = LOCK;
// critical section
locked = UNLOCK;
}
和 “山寨支付宝” 完全一样的错误
假设:内存的读/写可以保证顺序、原子完成
val = atomic_load(ptr)
atomic_store(ptr, val)
对应于 model checker
sys_sched()
A 和 B 争用厕所的包厢
进入临界区的情况
一些具体的细节情况
Prove by brute-force
void TA() { while (1) {
/* ❶ */ x = 1;
/* ❷ */ turn = B;
/* ❸ */ while (y && turn == B) ;
/* ❹ */ x = 0; } }
void TB() { while (1) {
/* ① */ y = 1;
/* ② */ turn = A;
/* ③ */ while (x && turn == A) ;
/* ④ */ y = 0; } }