gst_license_plate_recognition/include/CppThread.h
nqthai199@gmail.com df3dd9a705 first commit
2022-09-15 09:26:49 +07:00

71 lines
1.3 KiB
C++
Executable File

#ifndef __CPP_THREAD_H_
#define __CPP_THREAD_H_
/**
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* (C) 2020-2021, Bernd Porr <mail@bernporr.me.uk>
**/
#include <thread>
/**
* A thin wrapper around the C++ thread model to avoid
* a static callback. Instead just inherit this class
* and overload run() which then runs in this thread.
* This is header-only so that it can be performed
* inline for max performance.
**/
class CppThread {
public:
/**
* Starts the thread.
**/
inline void start() {
isRun = true;
if (mthread) return;
mthread=new std::thread([this]{this->run();});
}
inline void sleep(int time) {
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
/**
* Waits for the thread to terminate.
**/
inline void join() {
if (nullptr != mthread) {
mthread->join();
mthread = nullptr;
}
}
inline void stop() {
if (!mthread) return;
delete mthread;
}
inline bool isRunning() {
return this->isRun;
}
protected:
/**
* This method does all the work of this thread.
* Overload this abstract function with
* a real one doing the actual work of this thread.
**/
virtual void run() = 0;
private:
bool isRun = false;
// pointer to the thread
std::thread *mthread=NULL;
};
#endif