Faro Engine 0.0.0.b519570 (main)
Loading...
Searching...
No Matches
ThreadSafety.hpp
Go to the documentation of this file.
1#pragma once
2#include <mutex>
3
4namespace Faro
5{
6 /// @brief Synchronization flag to gain exclusive access on the calling thread.
7 class Mutex
8 {
9 public:
10 /// @brief Obtain exclusive access to this mutex.
11 void Lock();
12
13 /// @brief Release the previously claimed access.
14 void Unlock();
15
16 private:
17 std::mutex mutex;
18 };
19
20 /// @brief Container class providing thread-safe access to the internal object.
21 /// @tparam T Type of the contained data object
22 template<class T>
24 {
25 public:
26 ThreadSafe() : data() { }
27 ThreadSafe(T initialValue) : data(initialValue) { }
28
29 /// @brief Gain exclusive access to this object.
30 void Lock()
31 {
32 mutex.Lock();
33 }
34
35 /// @brief Release exclusive access to this object.
36 void Unlock()
37 {
38 mutex.Unlock();
39 }
40
41 /**
42 * @brief Set the value of this object.
43 * @param value New value of the object
44 */
45 void Set(T value)
46 {
47 Lock();
48 data = value;
49 Unlock();
50 }
51
52 /**
53 * @brief Get a copy of the contained value.
54 * @return T Copy of the contained value
55 */
57 {
58 Lock();
59 T returnValue = data;
60 Unlock();
61
62 return returnValue;
63 }
64
65 /**
66 * @brief Get a reference to the contained value.
67 * @warning Must be locked with #Lock and unlocked with #Unlock
68 * @return T& Reference to the contained value
69 */
70 T& Get()
71 {
72 return data;
73 }
74
75 /**
76 * @brief Get a pointer to the contained value.
77 * @warning Must be locked with #Lock and unlocked with #Unlock
78 * @return T* Pointer to the contained value
79 */
81 {
82 return &data;
83 }
84
85 private:
86 Mutex mutex;
87
88 T data;
89 };
90}
Synchronization flag to gain exclusive access on the calling thread.
Definition ThreadSafety.hpp:8
void Unlock()
Release the previously claimed access.
Definition ThreadSafety.cpp:10
void Lock()
Obtain exclusive access to this mutex.
Definition ThreadSafety.cpp:5
Container class providing thread-safe access to the internal object.
Definition ThreadSafety.hpp:24
T & Get()
Get a reference to the contained value.
Definition ThreadSafety.hpp:70
T * operator->()
Get a pointer to the contained value.
Definition ThreadSafety.hpp:80
void Lock()
Gain exclusive access to this object.
Definition ThreadSafety.hpp:30
ThreadSafe()
Definition ThreadSafety.hpp:26
T GetCopy()
Get a copy of the contained value.
Definition ThreadSafety.hpp:56
ThreadSafe(T initialValue)
Definition ThreadSafety.hpp:27
void Set(T value)
Set the value of this object.
Definition ThreadSafety.hpp:45
void Unlock()
Release exclusive access to this object.
Definition ThreadSafety.hpp:36
Definition Array.hpp:8