マルチスレッドプログラムに問題があります。一連のいくつかの整数配列 (通常は 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] に格納された 2D 配列) を取得しようとすると、関数は 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]);