Pol  Revision:cb584c9
spinlock.h
Go to the documentation of this file.
1 #ifndef CLIB_SPINLOCK_H
2 #define CLIB_SPINLOCK_H
3 
4 #include <atomic>
5 #include <mutex>
6 
7 namespace Pol
8 {
9 namespace Clib
10 {
17 class SpinLock
18 {
19  friend class std::lock_guard<SpinLock>;
20 
21 public:
22  SpinLock();
23  ~SpinLock();
24 
25 private:
26  void lock();
27  void unlock();
28 
29  std::atomic_flag _lck;
30 };
31 
33 typedef std::lock_guard<SpinLock> SpinLockGuard;
34 
36 {
37  _lck.clear();
38 }
40 
44 inline void SpinLock::lock()
45 {
46  while ( _lck.test_and_set( std::memory_order_acquire ) )
47  {
48  }
49 }
50 
54 inline void SpinLock::unlock()
55 {
56  _lck.clear( std::memory_order_release );
57 }
58 
59 } // namespace Clib
60 } // namespace Pol
61 #endif
std::atomic_flag _lck
Definition: spinlock.h:29
std::lock_guard< SpinLock > SpinLockGuard
Definition: spinlock.h:33
Definition: berror.cpp:12