forked from h31h31/H31DHTDEMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadWrapper.cpp
158 lines (140 loc) · 3.4 KB
/
ThreadWrapper.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/** @file ThreadWrapper.cpp
* @brief 线程封装类实现
*/
#include "stdafx.h"
#include "ThreadWrapper.h"
#if WIN32
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
//////////////////////////////////////////////////////////////////////
// Constructor/Destructor
//////////////////////////////////////////////////////////////////////
CThreadWrapper::CThreadWrapper()
{
// 初始化
m_hThreadHandle=NULL;
m_nPriority=100;
m_nStackSize=1000;
m_bStarted=false;
m_threadID=0;
memset(m_threadNameString,0,sizeof(m_threadNameString));
m_iError=0;
}
CThreadWrapper::~CThreadWrapper()
{
// 关闭
Close();
}
//////////////////////////////////////////////////////////////////////////
// public
bool CThreadWrapper::Start(ThreadFunc func,char* pName, int priority,int stackSize,void *param)
{
// 是否已经启动
if(m_bStarted)
{
_dout(_T("ThreadWrapper the thread already started!\n"));
m_iError=THD_OBJ_CREATED;
return false;
}
// 线程名
if (pName==NULL || (strcmp(pName,"")==0))
{
_dout(_T("ThreadWrapper input function name error!\n"));
m_iError=THD_NAME_ERROR;
return false;
}
sprintf_s(m_threadNameString,"%s",pName);
// 优先级有效性判断
if((priority < THREAD_PRIORITY_IDLE) || (priority > THREAD_PRIORITY_TIME_CRITICAL))
{
priority = THREAD_PRIORITY_NORMAL;
}
m_nPriority = priority;
// 栈大小
if((stackSize <= 0) || (stackSize > 100000000))
{
stackSize = 1000;
}
m_nStackSize=stackSize;
// 创建线程
m_hThreadHandle = CreateThread(0, // Security Attributes,
stackSize, // Stack Size,
func, // Starting Address.
param, // Parmeters
0, // Creation Flags,
&m_threadID); // Thread ID (Can not be null on 95/98)
if (m_hThreadHandle == 0)
{
_dout(_T("ThreadWrapper %s starting thread error,err(%d, %d)!\n"),m_threadNameString,GetLastError(), GetLastError());
m_iError=THD_CREATE_FAILED;
return false;
}
// 设置优先级
if(!SetThreadPriority(m_hThreadHandle,m_nPriority))
{
_dout(_T("ThreadWrapper %s set thread priority error,err(%d, %d)!\n"),m_threadNameString,GetLastError(), GetLastError());
m_iError=THD_PRI_FAILED;
}
// 获取线程优先级
m_nPriority=GetThreadPriority(m_hThreadHandle);
m_bStarted=true;
return true;
}
bool CThreadWrapper::IsExist()
{
// 返回结果
bool bRet=false;
// 退出code
unsigned long exitCode;
if(m_bStarted)
{
if(GetExitCodeThread(m_hThreadHandle,&exitCode)==TRUE)
{
// 进程还在
if(STILL_ACTIVE==exitCode)
{
bRet=true;
}
}
}
return bRet;
}
bool CThreadWrapper::Close(unsigned long waitTime,bool bForceClose)
{
if(m_bStarted)
{
// 释放线程句柄
if(m_hThreadHandle!=NULL)
{
_dout(_T("ThreadWrapper %s wait thread quit,wait time(%d).\n"), m_threadNameString,waitTime);
int nRes = ::WaitForSingleObject(m_hThreadHandle, waitTime);
// 等待到时间,线程没有退出,根据bForceClose决定是否强制退出
if (nRes != WAIT_OBJECT_0) {
_dout(_T("ThreadWrapper %s wait quit timeout,(err=%d).\n"), m_threadNameString,GetLastError());
if(bForceClose)
{
// 结束线程
if(TerminateThread(m_hThreadHandle,0)==FALSE)
{
_dout(_T("ThreadWrapper %s terminate thread failed,(err=%d).\n"), m_threadNameString,GetLastError());
}
}
}
if(m_hThreadHandle!=NULL)
{
CloseHandle(m_hThreadHandle);
m_hThreadHandle=NULL;
}
}
m_bStarted=false;
}
return true;
}
int CThreadWrapper::GetError()
{
return m_iError;
}