【发布时间】:2011-01-23 01:30:59
【问题描述】:
我正在编写一个程序,它使用两个不同的数据流来执行某些计算。我目前的代码设置如下:
//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 中的连续循环,无论队列中是否有新项目),但我不确定如何改进它。任何建议,将不胜感激。
【问题讨论】:
标签: c++ windows multithreading winapi