TTAS Lock C++11 实现
时间:2014-05-08 19:50:17
收藏:0
阅读:520
1 template<class Lock> 2 class lock_guard{ 3 public: 4 explicit lock_guard(Lock& lock){ 5 lock.lock(); 6 } 7 ~lock_guard(){ 8 lock.unlock(); 9 } 10 lock_guard(const lock_guard&) = delete; 11 lock_guard& operator = (const lock_guard&) = delete; 12 }; 13 14 class TTASLock{ 15 private: 16 atomic<bool> state; 17 public: 18 void lock(){ 19 while (true){ 20 while (state.load()){}; 21 if (!state.exchange(true, std::memory_order_relaxed)) 22 return; 23 } 24 } 25 void unlock(){ 26 state.store(false); 27 } 28 };
评论(0)