I have a problem with multithreaded program. Let's say I have series of few integer arrays (usually 2 or 3), each processed by a separate thread. I managed to make my computations, but now I would like to return processed array, created inside my thread.
After I start my threads, I start the following loop, which every 0.05 seconds checks for thread completion. This seems to work fine.
int partsPassed = 0;
int* partsCopied;
partsCopied = (int*) malloc (numThreads * sizeof(int));
int currentCopyStatus = 0;
for (n = 0; n < numThreads; n++) {
partsCopied[n] = 0;
}
// Loop until we copy all parts of array
while (partsPassed != numThreads) {
// Loop through all parts of original array
for (n = 0; n < numThreads; n++) {
if (partsCopied[n] != 1) {
// Check for finish of computation
CmtGetThreadPoolFunctionAttribute(*threadPoolHandle, threadFunctionID[n], ATTR_TP_FUNCTION_EXECUTION_STATUS, ¤tCopyStatus);
if (currentCopyStatus == kCmtThreadFunctionComplete) { // If yes, get the array and copy to original array
CmtGetThreadPoolFunctionAttribute(*threadPoolHandle, threadFunctionID[n], ATTR_TP_FUNCTION_RETURN_VALUE, imageThreadPart[n]);
copyStaticThreadResults(imageRecPart[n]->nrRowStart, imageRecPart[n]->nrRowEnd, imageThreadPart[n]);
partsCopied[n] = 1; // Copy flag display
partsPassed++; // Count all fragments
}
}
}
Delay(0.05);
}
The problem is that according to the documentation, I cannot get anything more than a mere int out of thread. This results in failure when I try to use the following function - I try to get int** (2D array stored at imageThreadPart[n]) and the function forces me to pass int*.
CmtGetThreadPoolFunctionAttribute(*threadPoolHandle, threadFunctionID[n], ATTR_TP_FUNCTION_RETURN_VALUE, imageThreadPart[n]);
1. Is it possible to use this function to obtain this array?
2. It might be a long shot, but is it possible to copy that array using callback of the following function and pass value returned by thread directly to this callback somehow?
CmtScheduleThreadPoolFunctionAdv (DEFAULT_THREAD_POOL_HANDLE, myThreadFunction, // thread function imageRecPart[n], // my data THREAD_PRIORITY_TIME_CRITICAL, copyThreadResults, // my callback EVENT_TP_THREAD_FUNCTION_END, NULL, // data for callback - but how to pass it from thread here?! CmtGetCurrentThreadID(), &threadFunctionID[n]);