59 lines
1.4 KiB
C++
59 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
namespace ddkit {
|
|
namespace base {
|
|
|
|
/// @brief wrapper for std::thread, including
|
|
/// - clean synchronized start/stop/stopCommanded-mechanism
|
|
/// - tracing with threadname, exception-logging on outermost level
|
|
class Thread {
|
|
|
|
public:
|
|
/// @param name 6-char-thread-name
|
|
Thread(const std::string& name);
|
|
virtual ~Thread();
|
|
|
|
void start();
|
|
// void stop(); // call stopAndJoin instead
|
|
void stopAndJoin();
|
|
|
|
bool isActive() const;
|
|
void waitForStopCommanded();
|
|
bool stopCommanded() const;
|
|
|
|
private:
|
|
std::string m_name;
|
|
std::unique_ptr<std::thread> m_thread;
|
|
bool m_threadExecutionActive;
|
|
|
|
std::mutex m_mutexStart;
|
|
std::condition_variable m_threadStarted;
|
|
bool m_hasStarted;
|
|
|
|
std::mutex m_mutexStop;
|
|
std::condition_variable m_threadStopped;
|
|
bool m_stopCommanded;
|
|
|
|
virtual void run();
|
|
virtual void onStop();
|
|
|
|
void startUp();
|
|
|
|
void waitForStarted();
|
|
virtual void waitForReadyIf();
|
|
|
|
// no copying
|
|
Thread(Thread const&) = delete;
|
|
Thread(Thread&&) = delete;
|
|
void operator=(Thread const&) = delete;
|
|
};
|
|
|
|
} // namespace base
|
|
} // namespace ddkit
|