I have a function that has to process all files in a set of directories (anything between 5-300 files). The number of parallel threads to be used is user-specified (usually 4). The idea is to start the function in 4 separate threads. When one thread returns, I have to start processing the next (5th) file and so on till all files are complete.
On Windows,
WaitForMultipleObjects()
with bWaitAll=False
helps me here. I have a structure that can be populated, and populated into an array
map::iterator iter = m_FileList.begin();
string outputPath = GetOutputPath();
void ***threadArgs = (void***)malloc(sizeof(void**)*numThreads);
HANDLE *hdl = (HANDLE*)malloc(sizeof(HANDLE)*numThreads);
DWORD *thr = (DWORD*)malloc(sizeof(DWORD)*numThreads);
for (int t = 0; iter != m_FileList.end() && t second, opPath);
printf("main: starting thread :%d %s outputPath: %s\n", t, iter->second.c_str(), threadArgs[t]);
hdl[t] = CreateThread(NULL, 0, fileProc, (void*)threadArgs[t], 0, &thr[t]);
if (hdl[t] == NULL)
{
err = GetLastError();
printf("main: thread failed %x %x %s %s\n", err, iter->second.c_str(), threadArgs[t]);
}
}
for (;iter != m_FileList.end(); iter++)
{
int t = (int)WaitForMultipleObjects(numThreads, hdl, FALSE, INFINITE);
if (t == WAIT_FAILED)
{
err = GetLastError();
printf("main: thread failed %x %x\n", t, err);
}
if (t - WAIT_OBJECT_0 >= 0 && t - WAIT_OBJECT_0 second, opPath);
printf("main: starting thread :%d %s outputPath: %s\n", t, iter->second.c_str(), threadArgs[t]);
hdl[t] = CreateThread(NULL, 0, fileProc, (void*)threadArgs[t], 0, &thr[t]);
if (hdl[t] == NULL)
{
err = GetLastError();
printf("main: thread failed %x %x %s %s\n", err, iter->second.c_str(), threadArgs[t]);
}
}
}
if (WAIT_FAILED == WaitForMultipleObjects(numThreads - 1, hdl, TRUE, INFINITE))
{
err = GetLastError();
printf("main: thread failed %x %x\n", err);
}
My problem now is to get similar functionality using pthreads. The best I can think of is to use semaphores and when one of them is available, spawn a new thread, and instead of using threadArgs array, I will use just one pointer that is allocated memory for every thread spawn. Also, for ease of memory management, the memory allocated for the threadArgs[t] will then be owned by the spawned thread.
Is there a better solution? Or is there something similar to WaitForMutlipleObjects()
with pthreads?
To put it more concretely, if I replace CreateThread()
with pthread_create()
, what should I replace WaitForMultipleObjects()
with?
Asked by tpb261
(135 rep)
Sep 16, 2020, 12:22 PM
Last activity: Sep 16, 2020, 11:59 PM
Last activity: Sep 16, 2020, 11:59 PM