2012-11-15 01:39:56 +02:00
|
|
|
#pragma once
|
|
|
|
|
2014-07-11 21:59:13 +10:00
|
|
|
static std::thread::id main_thread;
|
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
class NamedThreadBase
|
2012-11-15 01:39:56 +02:00
|
|
|
{
|
2013-11-28 05:16:19 +10:00
|
|
|
std::string m_name;
|
2012-11-15 01:39:56 +02:00
|
|
|
|
2014-06-20 15:00:36 +04:00
|
|
|
std::condition_variable m_signal_cv;
|
|
|
|
std::mutex m_signal_mtx;
|
|
|
|
|
2013-06-30 11:46:29 +03:00
|
|
|
public:
|
2014-01-31 20:40:18 +02:00
|
|
|
NamedThreadBase(const std::string& name) : m_name(name)
|
|
|
|
{
|
|
|
|
}
|
2012-11-15 01:39:56 +02:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
NamedThreadBase()
|
|
|
|
{
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
2013-11-28 05:16:19 +10:00
|
|
|
virtual std::string GetThreadName() const;
|
|
|
|
virtual void SetThreadName(const std::string& name);
|
2014-06-20 15:00:36 +04:00
|
|
|
|
2014-08-25 22:09:48 +04:00
|
|
|
void WaitForAnySignal();
|
2014-06-20 15:00:36 +04:00
|
|
|
|
2014-08-25 22:09:48 +04:00
|
|
|
void Notify();
|
2012-11-15 01:39:56 +02:00
|
|
|
};
|
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
NamedThreadBase* GetCurrentNamedThread();
|
2014-08-20 18:23:48 +04:00
|
|
|
void SetCurrentNamedThread(NamedThreadBase* value);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
class ThreadBase : public NamedThreadBase
|
2012-11-15 01:39:56 +02:00
|
|
|
{
|
2014-01-31 20:40:18 +02:00
|
|
|
protected:
|
|
|
|
std::atomic<bool> m_destroy;
|
|
|
|
std::atomic<bool> m_alive;
|
|
|
|
std::thread* m_executor;
|
2012-11-15 01:39:56 +02:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
mutable std::mutex m_main_mutex;
|
2013-06-30 11:46:29 +03:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
ThreadBase(const std::string& name);
|
|
|
|
~ThreadBase();
|
2012-11-15 01:39:56 +02:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
public:
|
|
|
|
void Start();
|
2014-02-14 20:50:02 +01:00
|
|
|
void Stop(bool wait = true, bool send_destroy = true);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
bool Join() const;
|
|
|
|
bool IsAlive() const;
|
|
|
|
bool TestDestroy() const;
|
2012-11-15 01:39:56 +02:00
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
virtual void Task() = 0;
|
2012-11-15 01:39:56 +02:00
|
|
|
};
|
|
|
|
|
2014-01-31 20:40:18 +02:00
|
|
|
class thread
|
|
|
|
{
|
|
|
|
std::string m_name;
|
|
|
|
std::thread m_thr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
thread(const std::string& name, std::function<void()> func);
|
|
|
|
thread(const std::string& name);
|
|
|
|
thread();
|
|
|
|
|
2014-02-19 19:27:52 +02:00
|
|
|
|
|
|
|
public:
|
2014-01-31 20:40:18 +02:00
|
|
|
void start(std::function<void()> func);
|
|
|
|
void detach();
|
|
|
|
void join();
|
|
|
|
bool joinable() const;
|
2014-08-25 22:09:48 +04:00
|
|
|
};
|