我有一个多线程程序的问题。假设我有一系列的几个整数数组(通常是 2 或 3 个),每个都由一个单独的线程处理。我设法进行了计算,但现在我想返回在我的线程中创建的已处理数组。
启动线程后,我启动以下循环,每 0.05 秒检查一次线程是否完成。这似乎工作正常。
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);
}
问题是,根据文档,我只能从线程中得到一个 int 。当我尝试使用以下函数时,这会导致失败 - 我尝试获取 int**(存储在 imageThreadPart[n] 中的二维数组)并且该函数强制我传递 int*。
CmtGetThreadPoolFunctionAttribute(*threadPoolHandle, threadFunctionID[n], ATTR_TP_FUNCTION_RETURN_VALUE, imageThreadPart[n]);
1、是否可以使用这个函数来获取这个数组?
2.这可能是一个长镜头,但是否可以使用以下函数的回调复制该数组并将线程返回的值直接传递给该回调?
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]);