-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.h
More file actions
60 lines (47 loc) · 1.62 KB
/
Thread.h
File metadata and controls
60 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include "Event.h"
#include "StlHelper.h"
#include <thread>
#include <atomic>
#include <mutex>
#include <unordered_map>
NS_BEGIN
using PFNPROC = void (*)(PVOID);
//#################################################################################################
class CThread
{
public:
explicit CThread(const CStr &strThreadName);
// Copy and move constructors
CThread(const CThread &src) = delete;
CThread(CThread &&src) = delete;
virtual ~CThread(void);
// Copy and move assignment operators
CThread &operator=(const CThread &src) = delete;
CThread &operator=(CThread &&src) = delete;
bool Start(void);
bool Start(PFNPROC pfnProc, PVOID pParam = nullptr);
void Wait(void);
void Reset(void);
const CStr &GetName(void) const noexcept;
std::thread::native_handle_type GetHandle(void) const;
std::thread::id GetId(void) const;
bool IsActive(void) const noexcept;
static PCNSTR GetThreadName(const std::thread::id &id = std::thread::id());
static bool MonitorThread(const CStr &strThreadName, PFNPROC pfnProc, PVOID pParam = nullptr);
private:
CStr m_strThreadName;
std::unique_ptr<std::thread> m_pThread;
mutable std::mutex m_mutexThread;
PFNPROC m_pfnProc;
PVOID m_pParam;
CEvent m_event;
std::atomic_bool m_bActive;
static std::unordered_map<size_t, PCNSTR, CNumberHashTraits<size_t>> s_mapThreads;
static std::mutex s_mutexThreads;
virtual void ThreadProc(void);
static bool AddThread(const size_t nThreadId, PCNSTR szThreadName);
static void RemoveThread(const size_t nThreadId);
static void InternalThreadProc(CThread *pThis);
};
NS_END