async_barrier.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 #ifndef PSEN_SCAN_V2_STANDALONE_ASYNC_BARRIER_H
16 #define PSEN_SCAN_V2_STANDALONE_ASYNC_BARRIER_H
17 
18 #include <future>
19 #include <thread>
20 #include <chrono>
21 #include <atomic>
22 
24 {
25 namespace util
26 {
30 class Barrier
31 {
32 public:
33  void release();
34  template <class Rep, class Period>
35  bool waitTillRelease(const std::chrono::duration<Rep, Period>& timeout) const;
36 
37 private:
38  std::promise<void> barrier_;
39  std::atomic_flag is_released_ = ATOMIC_FLAG_INIT;
40  const std::future<void> future_{ barrier_.get_future() };
41 };
42 
44 {
45  if (!is_released_.test_and_set())
46  {
47  barrier_.set_value();
48  }
49 }
50 
51 template <class Rep, class Period>
52 inline bool util::Barrier::waitTillRelease(const std::chrono::duration<Rep, Period>& timeout) const
53 {
54  return future_.wait_for(timeout) == std::future_status::ready;
55 }
56 
57 } // namespace util
58 } // namespace psen_scan_v2_standalone
59 
60 #endif // PSEN_SCAN_V2_STANDALONE_ASYNC_BARRIER_H
psen_scan_v2_standalone::util::Barrier
Helper class to simplify the synchronization between different threads.
Definition: async_barrier.h:30
psen_scan_v2_standalone::util::Barrier::future_
const std::future< void > future_
Definition: async_barrier.h:40
psen_scan_v2_standalone::util::Barrier::is_released_
std::atomic_flag is_released_
Definition: async_barrier.h:39
psen_scan_v2_standalone::util::Barrier::barrier_
std::promise< void > barrier_
Definition: async_barrier.h:38
psen_scan_v2_standalone::util::Barrier::waitTillRelease
bool waitTillRelease(const std::chrono::duration< Rep, Period > &timeout) const
Definition: async_barrier.h:52
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
psen_scan_v2_standalone::util::Barrier::release
void release()
Definition: async_barrier.h:43


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:11