//客户端 int main() { //店里添加烤肉师傅、菜单、服务员等顾客 Barbucer* barbucer=new Barbucer(); Command* cmd= new BakeMuttonCmd(barbucer); Command* cmd2=new ChickenWingCmd(barbucer); Waiter* girl = new Waiter(); //点菜 girl->SetCmd(cmd); girl->SetCmd(cmd2); //服务员通知 girl->Notify(); system("pause"); return 0; }
void Mutex::Unlock() { std::uint64_t head = 0; if (mWaiters.CompareAndSwap(head, MUTEX_FREE_BIT)) return; ExponentialBackoff backoff; for (;;) { CRUNCH_ASSERT_MSG((head & MUTEX_FREE_BIT) == 0, "Attempting to release unlocked mutex"); Waiter* headPtr = Detail::WaiterList::GetPointer(head); if (headPtr == nullptr) { CRUNCH_ASSERT((head & Detail::WaiterList::LOCK_BIT) == 0); // No waiters, attempt to set free bit. if (mWaiters.CompareAndSwap(head, MUTEX_FREE_BIT)) return; } else if (head & Detail::WaiterList::LOCK_BIT) { // Wait for lock release. head = mWaiters.Load(MEMORY_ORDER_RELAXED); } else { // Try to pop first waiter off list and signal std::uint64_t const newHead = Detail::WaiterList::SetPointer(head, headPtr->next) + Detail::WaiterList::ABA_ADDEND; if (mWaiters.CompareAndSwap(head, newHead)) { headPtr->Notify(); return; } } backoff.Pause(); } }