'How to keep blocking state to read message queue?

The message queue sender is started by a multi-timer. The message queue flag is blocking. I can't use a loop to read messages from the message queue because it takes too much resources. Is there any way that my function can call the msgrcv() to read the message as soon as there is a message in the message queue.Thanks a lot for your suggestions and answers! we use msgget、msgctl、msgsnd、msgrcv in our programm.

for(;;){
  msglen = msgrcv(serverId,&req, REQ_MSG_SIZE,0,0);
  if (msgLen ==-1){
    errMsg("msgrcv");
    break; 
  }
...
}

Here I use for to continuously call the msgrcv function to read the messages in the message queue.The cpu usage will reach 100%. is here a way for me to not use a for loop, when the message queue has a message msgrcv will be called and read the message.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source