-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHCPPThread.cpp
More file actions
86 lines (73 loc) · 1.94 KB
/
HCPPThread.cpp
File metadata and controls
86 lines (73 loc) · 1.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***************************************************************
* Name: HCPPThread.cpp
* Purpose: 实现HCPPThread接口
* Author: HYH (hyhsystem.cn)
* Created: 2026-04-08
* Copyright: HYH (hyhsystem.cn)
* License: MIT
**************************************************************/
#define HCPPTHREAD_IMPLEMENTATION 1
#include "HCPPThread.h"
HCPPThread::HCPPThread():HCPPObject(NULL),std::thread(std::bind(&HCPPThread::RunThread,this)),m_idle_sleep_time(std::chrono::milliseconds(1))
{
}
HCPPThread::~HCPPThread()
{
}
void HCPPThread::RunThread()
{
//HCPPOBject先于线程初始化,此时可使用HCPPObject的成员。
//等待初始化
while(!HasFlag(HCPPOBJECT_FLAG_RUN_INIT))
{
Run();
if (m_idle_sleep_time != std::chrono::milliseconds(0))
{
std::this_thread::sleep_for(m_idle_sleep_time);
}
else
{
std::this_thread::yield();
}
}
//HCPPThread类初始化完成,可使用类中的成员变量
while(!HasFlag(HCPPOBJECT_FLAG_TO_BE_DELETED))
{
Run();
if (m_idle_sleep_time != std::chrono::milliseconds(0))
{
std::this_thread::sleep_for(m_idle_sleep_time);
}
else
{
std::this_thread::yield();
}
}
//分离线程与对象
detach();
//删除自身
delete this;
}
//创建线程
HCPPThread *HCPPThread::New()
{
HCPPThread *ret=new HCPPThread();
if(ret!=NULL)
{
//设定线程信息为新创建的线程,表示线程对象的所有权归属新创建的线程
ret->SetThreadId(ret->get_id(),true);
//设定为可执行对象,此时Run函数开始生效
ret->SetRunable();
}
return ret;
}
//删除线程
void HCPPThread::DeleteThread()
{
//设置删除标志
DeleteLatter();
}
void HCPPThread::SetChildDelete(bool childdelete)
{
HCPPObject::SetChildDelete(childdelete);
}