modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / BGI / SOAPsnp / ThreadManager.cpp
blob5a18aa8d53aa0d149e6c9fd205bd8917e97699dc
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)
10 int nindex = 0;
11 pthread_mutex_lock(&mutex_ctrl);
12 if(param->pMgr->m_nindex < param->pMgr->m_nmaxthread)
14 nindex = param->pMgr->m_nindex++;
16 else
18 pthread_mutex_unlock(&mutex_ctrl);
19 break;
21 pthread_mutex_unlock(&mutex_ctrl);
22 param->pMgr->m_vproc[nindex](param->pMgr->m_vparam[nindex]);
24 return NULL;
27 ThreadManager::ThreadManager()
29 m_nindex = 0;
30 m_nmaxthread = 0;
31 m_ftimer = 0.1;
32 m_cpu = 1;
35 ThreadManager::ThreadManager( int cpu ):m_cpu(cpu)
37 m_nindex = 0;
38 m_nmaxthread = 0;
39 m_ftimer = 0.5;
41 if(m_cpu <= 0)
42 m_cpu = 1;
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)
51 delete m_vparam[i];
52 m_vparam[i] = NULL;
56 void ThreadManager::AddThread( pthreadproc pProc, void* param )
58 m_vproc.push_back(pProc);
59 m_vparam.push_back(param);
60 m_nmaxthread++;
63 void ThreadManager::Run()
65 if(m_cpu >= m_nmaxthread)
66 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;
72 vparam[i].flag = 0;
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*/ )
85 m_ftimer = t;
88 void ThreadManager::Reset()
90 m_ftimer = 1.0;
91 m_cpu = 1;
92 m_nmaxthread = 0;
93 m_nindex = 0;
95 // UPDATE at 2010-10-12
96 for (int i = 0; i < m_vparam.size(); ++i)
98 delete m_vparam[i];
99 m_vparam[i] = NULL;
101 m_vparam.clear();
102 m_vproc.clear();
105 void ThreadManager::SetCPU( int count )
107 m_cpu = count;
108 if (m_cpu >= m_nmaxthread) m_cpu = m_nmaxthread;