Search code examples
clinuxtimeoutmessage-queue

Example mq_timedreceive


I can not find how to work properly with mq_timedreceive, can anyone give me an example?

ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,
                   size_t msg_len, unsigned *msg_prio,
                   const struct timespec *abs_timeout);

I want timereceive to do not spent more than 20 seconds waiting.

Thank you very much.


Solution

  • struct   timespec tm;
    
    clock_gettime(CLOCK_REALTIME, &tm);
    tm.tv_sec += 20;  // Set for 20 seconds
    if( 0 > mq_timedreceive( fd, buf, 4096, NULL, &tm ) )  {
      ...
    }
    

    Take a look at the full description here