OpenCPN Partial API docs
Loading...
Searching...
No Matches
wait_continue.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2024 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
25#ifndef WAIT_COND__
26#define WAIT_COND__
27
28#include <cassert>
29#include <chrono>
30#include <condition_variable>
31#include <mutex>
32#include <thread>
33
34using namespace std::chrono_literals;
35
37public:
39 WaitContinue() : is_waiting(false) {}
40
41 WaitContinue& operator=(const WaitContinue) = delete;
42 WaitContinue(const WaitContinue&) = delete;
43
45 void Continue() {
46 {
47 std::unique_lock lock(m_mutex);
48 is_waiting = false;
49 }
50 cv.notify_all();
51 }
52
54 bool Wait(std::chrono::milliseconds timeout = 0s) {
55 std::unique_lock lock(m_mutex);
56 is_waiting = true;
57 auto t = std::chrono::high_resolution_clock::now() + timeout;
58 auto result = cv.wait_until(lock, t, [&] { return !is_waiting; });
59 return true;
60 }
61
63 void Wait(unsigned timeout_ms) {
64 Wait(std::chrono::milliseconds(timeout_ms));
65 }
66
67private:
68 bool is_waiting;
69 std::mutex m_mutex;
70 std::condition_variable cv;
71};
72
73#endif // WAIT_COND__
WaitContinue()
Default constructor, neither copyable nor assignable.
void Continue()
Release any threads blocked by Wait().
void Wait(unsigned timeout_ms)
Blocking wait for next Continue() or timeout (milliseconds)
bool Wait(std::chrono::milliseconds timeout=0s)
Blocking wait for next Continue() with optional timeout.