OpenCPN Partial API docs
Loading...
Searching...
No Matches
thread_ctrl.cpp
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#include "model/thread_ctrl.h"
26
28 std::lock_guard lock(m_mutex);
29 m_keep_going = 0;
30}
31
33 std::unique_lock lock(m_mutex);
34 m_cv.wait(lock, [&] { return m_keep_going < 0; });
35}
36
37bool ThreadCtrl::WaitUntilStopped(std::chrono::duration<int> timeout) {
38 std::unique_lock lock(m_mutex);
39 m_cv.wait_for(lock, timeout, [&] { return m_keep_going < 0; });
40 return m_keep_going < 0;
41}
42
43bool ThreadCtrl::WaitUntilStopped(std::chrono::duration<int> timeout,
44 std::chrono::milliseconds& elapsed) {
45 using namespace std::chrono;
46
47 auto start = steady_clock::now();
48 std::unique_lock lock(m_mutex);
49 m_cv.wait_for(lock, timeout, [&] { return m_keep_going < 0; });
50 auto end = steady_clock::now();
51 elapsed = duration_cast<milliseconds>(end - start);
52 return m_keep_going < 0;
53}
54
56 std::lock_guard lock(m_mutex);
57 return m_keep_going > 0;
58}
59
61 std::unique_lock lock(m_mutex);
62 m_keep_going = -1;
63 lock.unlock();
64 m_cv.notify_one();
65}
void WaitUntilStopped()
Block until thread invokes SignalExit().
void SignalExit()
Signal that thread has exited.
bool KeepGoing() const
If true continue thread operation, else exit and invoke SignalExit()
virtual void RequestStop()
Request that thread stops operation.
ThreadCtrl mixin class definition.