-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththreadmanager.cpp
More file actions
executable file
·93 lines (80 loc) · 1.63 KB
/
threadmanager.cpp
File metadata and controls
executable file
·93 lines (80 loc) · 1.63 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
87
88
89
90
91
92
93
#include "threadmanager.h"
pthread_mutex_t mutex_ctrl = PTHREAD_MUTEX_INITIALIZER;
void* __processwork(void* p)
{
THRPARAM* param = (THRPARAM*)p;
while(param->pMgr->m_nindex<param->pMgr->m_nmaxthread)
{
int nindex = 0;
pthread_mutex_lock(&mutex_ctrl);
if(param->pMgr->m_nindex<param->pMgr->m_nmaxthread)
{
nindex = param->pMgr->m_nindex++;
}
else
{
pthread_mutex_unlock(&mutex_ctrl);
break;
}
pthread_mutex_unlock(&mutex_ctrl);
param->pMgr->m_vproc[nindex](param->pMgr->m_vparam[nindex]);
}
}
CThreadManager::CThreadManager()
{
m_nindex = 0;
m_nmaxthread = 0;
m_ftimer = 0.1;
m_cpu = 1;
}
CThreadManager::CThreadManager( int cpu ):m_cpu(cpu)
{
m_nindex = 0;
m_nmaxthread = 0;
m_ftimer = 0.5;
if(m_cpu <= 0)m_cpu = 1;
}
CThreadManager::~CThreadManager()
{
}
void CThreadManager::AddThread( pthreadproc pProc, void* param )
{
m_vproc.push_back(pProc);
m_vparam.push_back(param);
m_nmaxthread++;
}
void CThreadManager::Run()
{
if(m_cpu >= m_nmaxthread)m_cpu = m_nmaxthread;
vector<pthread_t> vid(m_cpu);
vector<THRPARAM> vparam(m_cpu);
for (int i=0; i<m_cpu; i++)
{
vparam[i].pMgr = this;
vparam[i].flag = 0;
pthread_create(&vid[i], NULL, __processwork, (void*)&vparam[i]);
usleep(int(m_ftimer*float(1000000)));
}
for (int i=0; i<m_cpu; i++)
{
pthread_join(vid[i], NULL);
}
}
void CThreadManager::SetTimer( float t /*= 0.5*/ )
{
m_ftimer = t;
}
void CThreadManager::Reset()
{
m_ftimer = 1.0;
m_cpu = 1;
m_nmaxthread = 0;
m_nindex = 0;
m_vparam.clear();
m_vproc.clear();
}
void CThreadManager::SetCPU( int count )
{
m_cpu = count;
if (m_cpu >= m_nmaxthread) m_cpu = m_nmaxthread;
}