-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
85 lines (58 loc) · 1.18 KB
/
Copy pathThread.cpp
File metadata and controls
85 lines (58 loc) · 1.18 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
#include "Thread.h"
#include <unistd.h>
#ifndef TRACE
#define TRACE OutputDebugString
#endif
CThread::CThread(const char *szThreadName)
{
m_hThread = -1;
m_bThreadRunning = false;
m_bThreadShouldStop = false;
if(szThreadName != NULL)
{
m_ThreadName = szThreadName;
}
}
CThread::~CThread(void)
{
}
bool CThread::Start()
{
m_pThis = this;
m_bThreadRunning = true;
return pthread_create(&m_hThread, NULL, &CThread::staticThread, this) == 0;
}
bool CThread::ShouldExit(int delay)
{
usleep(delay * 1000);
return m_bThreadShouldStop;
}
void CThread::Stop(sem_t* sem)
{
m_bThreadShouldStop = true;
if(sem)
{
sem_post(sem);
}
while(m_bThreadShouldStop)
{
usleep(100);
}
}
void CThread::DeleteHandles()
{
}
int CThread::Thread()
{
int returnValue = Run();
m_bThreadRunning = false;
return returnValue;
}
void* CThread::staticThread(void* lpParameters)
{
const CThread *pParameters = (const CThread*) lpParameters;
(void*)pParameters->m_pThis->Thread();
pParameters->m_pThis->m_bThreadRunning = false;
pParameters->m_pThis->m_bThreadShouldStop = false;
return lpParameters;
}