私はある計算を実行するために2つの異なるデータストリームを使用するプログラムを書いています。私は現在、私のコードは次のように設定している:複数のデータストリームから読み取るプログラムを設計するにはどうすればよいですか?
//pseudo c++
class Calculator
{
//Stream is a class which contains a queue which has new messages added to it
Stream a, b;
bool BothQueuesHaveItems() { return !a.IsEmpty() && !b.IsEmpty(); }
void PerformCalc() { /*Do domething with the first items in a and b*/ }
};
unsigned ThreadFunction(void *stop_event)
{
Calculator calculator;
while(WaitForSingleObject(stop_event) == WAIT_OBJECT_0)
{
if(calculator.BothQueuesHaveItems())
{
calculator.PerformCalc();
}
Sleep(5);
}
}
int main()
{
Event stop_event = CreateEvent(...);
CreateThread(ThreadFunction, &stop_event, ...);
//wait for user enter command to stop processing
SetEvent(stop_event);
}
これは非常に良いデザイン(キューまたはないで新しいアイテムがあるかどうかThreadFunctionで特に連続ループ)のように見えるしていませんが、私は」それを改善する方法が正確にはわかりません。任意の提案をいただければ幸いです。