基于CAS实现自旋锁


#include<aomic>

class SpinLock{
public:
    SpinLock():flag_(false){}
    void lock(){
      bool except = false;
      while(!flag_.compare_exchange_weak(expect,true))
      {  
        expect = false;
      }

    }

void unlock()
{
  flag_.store(false);
}
private:
std::atmoic<bool>flag_;
  };

posted @ 2023-06-20 23:40  simp00  阅读(29)  评论(0编辑  收藏  举报