下面的代码有3处常见错误:
ACE_Message_Queue<ACE_NULL_SYNCH> high_priority_queue, low_priority_queue; ACE_Message_Block *mb; while (1) { // 超时时间200毫秒 ACE_Time_Value time_out (0, 200); // 优先处理高优先级队列中的消息 while (high_priority_queue.dequeue (mb, &time_out) != -1) { process (mb); mb->release (); } // 高优先级队列处理完毕后,处理高优先级队列中的消息 while (low_priority_queue.dequeue (mb, &time_out) != -1) { process (mb); mb->release (); } }
- ACE_Time_Value构造函数第二个参数单位为微秒,200毫秒应表示成ACE_Time_Value time_out (0, 200*1000);
- dequeue函数的超时参数使用绝对时间,而不是相对时间;
- 由于队列类型模版参数为ACE_NULL_SYNCH,即使dequeue指定了超时参数,也不会等待超时,而会直接返回,上面的代码段是一个耗尽CPU的死循环!!