Advertisement
MystMe

Untitled

Feb 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <atomic.hpp>
  4. #include <backoff.hpp>
  5.  
  6. namespace tpcc {
  7. namespace solutions {
  8.  
  9. class TicketLock {
  10.  public:
  11.   // don't change this method
  12.  
  13.   void Lock() {
  14.     Backoff backoff{};
  15.     while (locked.exchange(true)) {
  16.       backoff();
  17.     }
  18.   }
  19.  
  20.   bool TryLock() {
  21.     return !locked.exchange(true);
  22.   }
  23.  
  24.   // don't change this method
  25.   void Unlock() {
  26.     locked.store(false);
  27.   }
  28.  
  29.  private:
  30.    atomic<bool> locked;
  31. };
  32.  
  33. } // namespace solutions
  34. } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement