SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Lock.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (C) 2013 Soeren Sonnenburg
8  */
9 #ifndef __LOCK_H__
10 #define __LOCK_H__
11 
12 #include <shogun/lib/config.h>
13 #include <shogun/lib/cpu.h>
14 #include <atomic>
15 
16 namespace shogun
17 {
19 class CLock
20 {
21 public:
24  {
25  do
26  {
27  while (m_locked.load(std::memory_order_relaxed))
28  CpuRelax();
29  }
30  while (m_locked.exchange(true, std::memory_order_acquire));
31  }
32 
35  {
36  m_locked.store(false, std::memory_order_release);
37  }
38 
39 private:
41  std::atomic_bool m_locked = { false };
42 };
43 }
44 #endif // __LOCK_H__
#define SG_FORCED_INLINE
Definition: common.h:91
Class Lock used for synchronization in concurrent programs.
Definition: Lock.h:19
SG_FORCED_INLINE void lock()
Definition: Lock.h:23
static SG_FORCED_INLINE void CpuRelax()
Definition: cpu.h:36
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SG_FORCED_INLINE void unlock()
Definition: Lock.h:34

SHOGUN Machine Learning Toolbox - Documentation