1 #include "ThreadManager.h"
3 pthread_mutex_t mutex_ctrl
= PTHREAD_MUTEX_INITIALIZER
;
5 void *__processwork(void* p
)
6 { THRPARAM
* param
= (THRPARAM
*)p
;
8 while(param
->pMgr
->m_nindex
< param
->pMgr
->m_nmaxthread
)
11 pthread_mutex_lock(&mutex_ctrl
);
12 if(param
->pMgr
->m_nindex
< param
->pMgr
->m_nmaxthread
)
14 nindex
= param
->pMgr
->m_nindex
++;
18 pthread_mutex_unlock(&mutex_ctrl
);
21 pthread_mutex_unlock(&mutex_ctrl
);
22 param
->pMgr
->m_vproc
[nindex
](param
->pMgr
->m_vparam
[nindex
]);
27 ThreadManager::ThreadManager()
35 ThreadManager::ThreadManager( int cpu
):m_cpu(cpu
)
45 ThreadManager::~ThreadManager()
47 // UPDATE at 2010-10-21
48 for (int i
= 0; i
< m_vparam
.size(); ++i
)
50 if (m_vparam
[i
] != NULL
)
56 void ThreadManager::AddThread( pthreadproc pProc
, void* param
)
58 m_vproc
.push_back(pProc
);
59 m_vparam
.push_back(param
);
63 void ThreadManager::Run()
65 if(m_cpu
>= m_nmaxthread
)
67 vector
<pthread_t
> vid(m_cpu
);
68 vector
<THRPARAM
> vparam(m_cpu
);
69 for (int i
=0; i
<m_cpu
; i
++)
71 vparam
[i
].pMgr
= this;
73 pthread_create(&vid
[i
], NULL
, __processwork
, (void*)&vparam
[i
]);
74 usleep(int(m_ftimer
*float(1000000)));
77 for (int i
=0; i
<m_cpu
; i
++)
79 pthread_join(vid
[i
], NULL
);
83 void ThreadManager::SetTimer( float t
/*= 0.5*/ )
88 void ThreadManager::Reset()
95 // UPDATE at 2010-10-12
96 for (int i
= 0; i
< m_vparam
.size(); ++i
)
105 void ThreadManager::SetCPU( int count
)
108 if (m_cpu
>= m_nmaxthread
) m_cpu
= m_nmaxthread
;