0

breakキューが空でない場合にループが実行されるループを作成していますが、ループの最後にa を含める必要があるかどうか疑問に思っていました。基本的に、各ループは、キューが空になるまで、キュー内のすべての要素に対して実行する必要があります。

では、次のうちどれを実行する必要がありますか。正しいことがあるかどうかはわかりません。

while (1)
{

    /*process message from incoming queue*/
    if (!msgs_inc.empty())
    {
        /*categorise incoming message into global map of outgoing messages*/
        msgInfo current_msg = incMsgClassification(msgs_inc.front());
        msgs_inc.pop();

        clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
    }
}

また

while (1)
{
    //Sleep(50000);
    //cout << "success" << endl;

    /*process message from incoming queue*/
    if (!msgs_inc.empty())
    {
        /*categorise incoming message into global map of outgoing messages*/
        msgInfo current_msg = incMsgClassification(msgs_inc.front());
        msgs_inc.pop();

        clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
        break;
    }
}
4

1 に答える 1

8

あなたがしたいことは、よりきれいに書かれています..

while (!msgs_inc.empty()) // loop as long as queue still has elements
{    
    /*process message from incoming queue*/
    /*categorise incoming message into global map of outgoing messages*/
    msgInfo current_msg = incMsgClassification(msgs_inc.front());
    msgs_inc.pop();

    clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
}

多分

while(1) {//infinite loop
    while (!msgs_inc.empty()) // loop as long as queue still has elements
    {    
        /*process message from incoming queue*/
        /*categorise incoming message into global map of outgoing messages*/
        msgInfo current_msg = incMsgClassification(msgs_inc.front());
        msgs_inc.pop();

        clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
    }
}

これは、この関数が別のスレッドで実行されており、そのスレッドで実行されている唯一のコードである場合に、より関連性があります。

于 2013-03-12T08:33:42.153 に答える