私は DCPU-16 エミュレーターを作成しており、別のスレッドで関数 getRealTimeCPUClock() を呼び出すスレッドを起動して、CPU のリアルタイム クロック速度を計算しています。問題は、値が返されていない場合でも、future オブジェクトの「valid」属性が true であるように見えることです。その結果、futureObj.get() を呼び出すと、getRealTimeCPUClock() が戻るまで待機します。
非同期の起動ポリシー (遅延ではなく) では、関数をバックグラウンドで起動し、返されたときに有効な属性を true に設定することは想定されていませんか?
これは間違った使い方ですか?
int getRealTimeCPUClock() {
int cyclesBeforeTimer = totalCycles;
sleep(1);
return totalCycles - cyclesBeforeTimer;
}
void startExecutionOfProgram(char *programFileName)
{
size_t lengthOfProgramInWords = loadProgramIntoRAM(programFileName);
auto futureRealTimeClockSpeed = std::async(std::launch::async, getRealTimeCPUClock);
while(programCounter < lengthOfProgramInWords) {
if(futureRealTimeClockSpeed.valid()) {
realTimeClockSpeed = futureRealTimeClockSpeed.get();
futureRealTimeClockSpeed = std::async(std::launch::async, getRealTimeCPUClock);
}
step();
}
}