DESERT 4.0.0
stoppable_thread.h
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#pragma once
40
41#include <atomic>
42#include <iostream>
43#include <thread>
44using namespace std::chrono_literals;
45
50{
51public:
52 StoppableThread() = default;
53 virtual ~StoppableThread() = default;
58 bool
59 Start(bool exc_info = false)
60 {
61 /* Check for double-start */
62 if (Running())
63 return false;
64 try {
65 m_thread = std::thread(&StoppableThread::RunInternal, this);
66 return true;
67 } catch (std::exception &ex) {
68 if (exc_info)
69 std::cerr << "StoppableThread::Start() exception: " << ex.what()
70 << std::endl;
71 } catch (...) {
72 if (exc_info)
73 std::cerr << "StoppableThread::Start() unknow exception catched"
74 << std::endl;
75 }
76 return false;
77 }
78
84 void
85 Stop(bool wait = false)
86 {
87 m_stop.store(true);
88 if (wait && m_thread.joinable()) {
89 try {
90 m_thread.join();
91 } catch (...) {
92 }
93 }
94 }
106 virtual void Run() = 0;
107
109 template <class Rep, class Period>
110 void
111 Sleep(const std::chrono::duration<Rep, Period> &d)
112 {
113 std::this_thread::sleep_for(d);
114 }
118 bool
120 {
121 return m_running.load();
122 }
126 bool
128 {
129 return m_stop.load();
130 }
131
132private:
134 void
136 {
137 m_running = true;
138 Run();
139 m_running = false;
140 }
141 std::atomic_bool m_running{false};
142 std::atomic_bool m_stop{false};
143 std::thread m_thread;
144};
Definition stoppable_thread.h:50
std::atomic_bool m_stop
Definition stoppable_thread.h:142
virtual void Run()=0
std::thread m_thread
Definition stoppable_thread.h:143
void Sleep(const std::chrono::duration< Rep, Period > &d)
Definition stoppable_thread.h:111
bool Running()
Definition stoppable_thread.h:119
bool Start(bool exc_info=false)
Definition stoppable_thread.h:59
virtual ~StoppableThread()=default
void RunInternal()
Definition stoppable_thread.h:135
void Stop(bool wait=false)
Definition stoppable_thread.h:85
bool StopRequested()
Definition stoppable_thread.h:127
StoppableThread()=default
std::atomic_bool m_running
Definition stoppable_thread.h:141