FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
thread.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9
10#include <condition_variable>
11#include <mutex>
12#include <thread>
13
14namespace FEAT
15{
36 {
37 private:
39 std::mutex _mtx;
41 std::condition_variable _cvar;
43 bool _open;
45 bool _okay;
46
47 public:
49 explicit ThreadFence() : _open(false), _okay(false) {}
50
52 ThreadFence(const ThreadFence&) = delete;
55
68 bool wait()
69 {
70 std::unique_lock<std::mutex> lock(_mtx);
71 // we must to use a while loop here to correctly handle spurious wake-ups
72 while(!_open)
73 _cvar.wait(lock);
74 return _okay;
75 }
76
86 void open(bool okay = true)
87 {
88 std::unique_lock<std::mutex> lock(_mtx);
89 _open = true;
90 _okay = okay;
91 _cvar.notify_all();
92 }
93
97 void close()
98 {
99 std::unique_lock<std::mutex> lock(_mtx);
100 _open = _okay = false;
101 }
102 }; // class ThreadFence
103} // namespace FEAT
FEAT Kernel base header.
Thread fence synchronization utility class.
Definition: thread.hpp:36
ThreadFence()
constructor
Definition: thread.hpp:49
std::mutex _mtx
the internal mutex
Definition: thread.hpp:39
void open(bool okay=true)
Open the fence and notify all waiting threads.
Definition: thread.hpp:86
bool wait()
Wait for the fence to be opened.
Definition: thread.hpp:68
ThreadFence & operator=(const ThreadFence &)=delete
delete copy-assignment operator
bool _open
current state of the fence: open or closed
Definition: thread.hpp:43
std::condition_variable _cvar
the internal condition variable
Definition: thread.hpp:41
ThreadFence(const ThreadFence &)=delete
delete copy-constructor
bool _okay
additional state variable
Definition: thread.hpp:45
void close()
Close the fence and reset internal state.
Definition: thread.hpp:97
FEAT namespace.
Definition: adjactor.hpp:12