-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWin_Thread.cpp
More file actions
52 lines (43 loc) · 999 Bytes
/
Copy pathWin_Thread.cpp
File metadata and controls
52 lines (43 loc) · 999 Bytes
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
#include "StdAfx.h"
#include <atlbase.h>
#include "Win_Thread.h"
using namespace DeviceDetectLibrary;
CThread::CThread(ThreadFunction routine)
: CWaitObject(INVALID_HANDLE_VALUE, CAutoHandle::Nothrow())
, routine_(routine)
, currentState_(NULL)
{
}
CThread::~CThread(void)
{
Wait(INFINITE);
}
void CThread::Start(void * state)
{
if((HANDLE)(*this) != INVALID_HANDLE_VALUE)
{
throw CAutoHandleException("Thread is already started");
}
currentState_ = state;
Reset((HANDLE)_beginthreadex(NULL, 0, CThread::Routine, this, 0, NULL));
if(((HANDLE)(*this) == INVALID_HANDLE_VALUE) || ((HANDLE)(*this) == NULL))
{
Reset();
throw CAutoHandleException("Cannot create thread");
}
}
unsigned int __stdcall CThread::Routine(void * state)
{
CThread* pThis = static_cast<CThread*>(state);
pThis->routine_(pThis->currentState_);
return 0;
}
bool CThread::Wait(unsigned int milliseconds)
{
bool result = CWaitObject::Wait(milliseconds);
if(result)
{
Reset();
}
return result;
}