DJ_AM_Juicebox
asked on
translating some win32 thread stuff to pthreads
Hi,
I have this bit of code that I've been using for awhile which creates a thread pool of 100 threads. When requests come in to create a new thread, the new thread is created only if there is an open slot in the thread pool. Otherwise the function waits for a slot to become available and will create the thread in that slot:
MyClass::MyClass()
{
m_nOpenSlot = 0;
m_nOpenThreads = 0;
m_vThreads.resize(100, 0); // vector<HANDLE>, the thread pool.
}
int MyClass::GetMaxNumberOfAll owedThread s()
{
return 100;
}
void MyClass::CreateNewThread()
{
// If there are already 100 threads open, wait until one finishes then use that opened slot.
if (m_nOpenThreads >= GetMaxNumberOfAllowedThrea ds()) {
DWORD dwRes = WaitForMultipleObjects(Get MaxNumberO fAllowedTh reads(), &m_vThreads[0], FALSE, INFINITE);
m_nOpenSlot = dwRes - WAIT_OBJECT_0;
m_nOpenThreads--;
CloseHandle(m_vThreads[m_n OpenSlot]) ;
}
// Let derived classes create their own specialized thread data to handle the association.
// It is treated as a base class type.
SomeData* p = new SomeData();
m_vThreads[m_nOpenSlot] = (HANDLE)_beginthreadex( (void *) 0,
(unsigned) 0,
(PTHREAD_START) &ThreadFunction,
(void *) p,
(unsigned) 0,
0);
m_nOpenSlot++;
m_nOpenThreads++;
}
I don't know what the equivalents is in pthreads for:
DWORD dwRes = WaitForMultipleObjects(Get MaxNumberO fAllowedTh reads(), &m_vThreads[0], FALSE, INFINITE);
m_nOpenSlot = dwRes - WAIT_OBJECT_0;
is there some one to one mapping for it or a completely new methodology?
Thanks
I have this bit of code that I've been using for awhile which creates a thread pool of 100 threads. When requests come in to create a new thread, the new thread is created only if there is an open slot in the thread pool. Otherwise the function waits for a slot to become available and will create the thread in that slot:
MyClass::MyClass()
{
m_nOpenSlot = 0;
m_nOpenThreads = 0;
m_vThreads.resize(100, 0); // vector<HANDLE>, the thread pool.
}
int MyClass::GetMaxNumberOfAll
{
return 100;
}
void MyClass::CreateNewThread()
{
// If there are already 100 threads open, wait until one finishes then use that opened slot.
if (m_nOpenThreads >= GetMaxNumberOfAllowedThrea
DWORD dwRes = WaitForMultipleObjects(Get
m_nOpenSlot = dwRes - WAIT_OBJECT_0;
m_nOpenThreads--;
CloseHandle(m_vThreads[m_n
}
// Let derived classes create their own specialized thread data to handle the association.
// It is treated as a base class type.
SomeData* p = new SomeData();
m_vThreads[m_nOpenSlot] = (HANDLE)_beginthreadex( (void *) 0,
(unsigned) 0,
(PTHREAD_START) &ThreadFunction,
(void *) p,
(unsigned) 0,
0);
m_nOpenSlot++;
m_nOpenThreads++;
}
I don't know what the equivalents is in pthreads for:
DWORD dwRes = WaitForMultipleObjects(Get
m_nOpenSlot = dwRes - WAIT_OBJECT_0;
is there some one to one mapping for it or a completely new methodology?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks