iMSTK
Interactive Medical Simulation Toolkit
imstkSpinLock.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include <atomic>
10 
11 namespace imstk
12 {
13 namespace ParallelUtils
14 {
20 class SpinLock
21 {
22 public:
27  {
28  unlock();
29  }
30 
37  {
38  unlock();
39  }
40 
45  void lock()
46  {
47  while (m_Lock.test_and_set(std::memory_order_acquire)) {}
48  }
49 
53  void unlock()
54  {
55  m_Lock.clear(std::memory_order_release);
56  }
57 
58 private:
59  std::atomic_flag m_Lock;
60 };
61 } // end namespace ParallelUtils
62 } // end namespace imstk
The SpinLock class.
Definition: imstkSpinLock.h:20
void unlock()
End a thread-safe region.
Definition: imstkSpinLock.h:53
Compound Geometry.
SpinLock(const SpinLock &)
Copy constructor, must be implemented as an empty function because the member variable of type std::a...
Definition: imstkSpinLock.h:36
void lock()
Start a thread-safe region, where only one thread can execute at a time until a call to the unlock fu...
Definition: imstkSpinLock.h:45
SpinLock()
Default constructor, initializes the atomic_flag member to memory_order_release state.
Definition: imstkSpinLock.h:26