(所以不上锁不就没有死锁了吗?)
void T_1() { spin_lock(&A); sum++; spin_unlock(&A); } void T_2() { spin_lock(&B); sum++; spin_unlock(&B); }
void T_1() { spin_lock(&A); sum++; spin_unlock(&A); } void T_2() { sum++; }
void wait_next_beat(int expect) { // This is a spin-wait loop. retry: mutex_lock(&lk); // This read is protected by a mutex. int got = n; mutex_unlock(&lk); // Case 2: 忘记上锁 if (n != expect) goto retry; }
用锁保护好共享数据
消灭一切数据竞争