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 };
TTAS Lock C++11 实现,布布扣,bubuko.com
原文:http://www.cnblogs.com/zxh1210603696/p/3709559.html