threads and monitor
This commit is contained in:
84
src/Core/Threads.cpp
Normal file
84
src/Core/Threads.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
------------------------------------------------------------------
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#include "Threads.h"
|
||||
#include <chrono>
|
||||
|
||||
namespace uLib {
|
||||
|
||||
Thread::Thread() : m_Running(false) {}
|
||||
|
||||
Thread::~Thread() {
|
||||
if (m_Thread.joinable()) {
|
||||
m_Thread.detach();
|
||||
}
|
||||
}
|
||||
|
||||
void Thread::Start() {
|
||||
Mutex::ScopedLock lock(m_ThreadMutex);
|
||||
if (m_Running) return;
|
||||
|
||||
m_Running = true;
|
||||
m_Thread = std::thread(&Thread::ThreadEntryPoint, this);
|
||||
}
|
||||
|
||||
void Thread::Join() {
|
||||
if (m_Thread.joinable()) {
|
||||
m_Thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void Thread::Detach() {
|
||||
if (m_Thread.joinable()) {
|
||||
m_Thread.detach();
|
||||
}
|
||||
}
|
||||
|
||||
bool Thread::IsJoinable() const {
|
||||
return m_Thread.joinable();
|
||||
}
|
||||
|
||||
bool Thread::IsRunning() const {
|
||||
return m_Running;
|
||||
}
|
||||
|
||||
void Thread::Run() {
|
||||
// Override in subclasses
|
||||
}
|
||||
|
||||
void Thread::ThreadEntryPoint() {
|
||||
this->Run();
|
||||
m_Running = false;
|
||||
}
|
||||
|
||||
void Thread::Sleep(int milliseconds) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
||||
}
|
||||
|
||||
void Thread::Yield() {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
} // namespace uLib
|
||||
Reference in New Issue
Block a user