【发布时间】:2015-08-27 13:22:08
【问题描述】:
我遇到了多线程程序的问题。假设我有一系列的几个整数数组(通常是 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]);
【问题讨论】:
-
请注意,线程不能“返回”某些东西,因为它没有被“调用”(两者都与应用程序有关,而不是线程库)。使用线程时不要轮询!在你的主线程中使用一个队列来收集所有已完成线程的数据。或者,如果数据已由主线程拥有,则计数信号量将起作用。
-
"我印象深刻......" 只是一个简单的思想实验:如果你调用函数来启动线程:它是否在启动的线程仍在运行时返回? - 是的。那么,如何,何时 - 以及 where - 您是否将“结果”放入启动另一个步骤的线程中?欢迎来到异步(和并行)编程的奇妙世界。请系好安全带,周围都是焦油坑等着不知情的人;-)(OTOH如果做得对,真的很迷人)。
标签: c arrays multithreading cvi