libtasks Documentation  1.6
spinlock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _SPINLOCK_H_
9 #define _SPINLOCK_H_
10 
11 #include <atomic>
12 
13 namespace tasks {
14 namespace tools {
15 
16 class spinlock {
17  public:
18  spinlock() : m_lock(false) {}
19 
20  inline void lock() {
21  while (m_lock.exchange(true)) {
22  }
23  }
24 
25  inline void unlock() { m_lock = false; }
26 
27  private:
28  std::atomic<bool> m_lock;
29 };
30 
31 } // tools
32 } // tasks
33 
34 #endif // _SPINLOCK_H_
std::atomic< bool > m_lock
Definition: spinlock.h:28