atomic_lock.h 298 B

123456789101112131415161718192021222324
  1. #ifndef _ATOMIC_LOCK_HPP_
  2. #define _ATOMIC_LOCK_HPP_
  3. #include <atomic>
  4. struct atomic_lock
  5. {
  6. std::atomic<int>&mutex_;
  7. atomic_lock (std::atomic<int>&mutex)
  8. :mutex_(mutex)
  9. {
  10. int ex=0;
  11. while(!mutex_.compare_exchange_weak(ex,1))
  12. {
  13. }
  14. }
  15. ~atomic_lock()
  16. {
  17. mutex_.store(0);
  18. }
  19. };
  20. #endif