DESERT 3.5.1
Loading...
Searching...
No Matches
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 Start(bool exc_info = false)
59 {
60 /* Check for double-start */
61 if (Running())
62 return false;
63 try {
64 m_thread = std::thread(&StoppableThread::RunInternal, this);
65 return true;
66 }
67 catch (std::exception& ex) {
68 if (exc_info)
69 std::cerr << "StoppableThread::Start() exception: " << ex.what() << std::endl;
70 }
71 catch (...) {
72 if (exc_info)
73 std::cerr << "StoppableThread::Start() unknow exception catched" << std::endl;
74 }
75 return false;
76 }
77
82 void Stop(bool wait = false)
83 {
84 m_stop.store(true);
85 if (wait && m_thread.joinable())
86 {
87 try
88 {
89 m_thread.join();
90 }
91 catch (...)
92 {
93 }
94 }
95 }
106 virtual void Run() = 0;
107
109 template <class Rep, class Period>
110 void Sleep(const std::chrono::duration<Rep, Period> &d)
111 {
112 std::this_thread::sleep_for(d);
113 }
117 bool Running() { return m_running.load(); }
121 bool StopRequested() { return m_stop.load(); }
122
123private:
126 {
127 m_running = true;
128 Run();
129 m_running = false;
130 }
131 std::atomic_bool m_running{false};
132 std::atomic_bool m_stop{false};
133 std::thread m_thread;
134};
A stoppable C++11 thread implementation.
std::atomic_bool m_stop
virtual void Run()=0
Pure virtual stopable worker function of thread, use the test StopRequested() to check if it should s...
void Sleep(const std::chrono::duration< Rep, Period > &d)
Sleep for the given duration, use literals like 1s, 100ms, 10us.
bool Running()
Returns the current state of the thread.
bool Start(bool exc_info=false)
Start the thread.
virtual ~StoppableThread()=default
void RunInternal()
Internal thread function.
void Stop(bool wait=false)
Stop the thread, needs call(s) to StopRequested() in the Run() worker function to check for the stop ...
bool StopRequested()
Returns if a stop was requested.
StoppableThread()=default
std::atomic_bool m_running