boost::interprocess message queue timed_receive() internal procedure

可紊 提交于 2021-01-29 02:28:22

问题


im currently using the timed_receive() method from the boost::interprocess library for receiving data. Since the timing of the received messages will vary I used this method over the receive() method.

msgque->timed_receive((void*) &message,sizeof(int),recvd_size,priority,
            boost::posix_time::ptime(microsec_clock::universal_time()) + boost::posix_time::milliseconds(300))

Question: How does this method know a message is present in the buffer? Is it a polling mechanism or is there a more complex mechanism implemented? I read the documentation and couldnt find any details and the source code wasnt informative either.

Thanks already.


回答1:


The library doesn't need to document how it works, because that's an implementation detail. You ideally do not need to know, which is why you use a library in the first place.

You can expect the library to implement it in terms of more primitive library building blocks:

  • shared_memory_object + mapped_region
  • mutex, + condition

That implies that the condition gets signaled by another process.

However it's also possible that something more advanced / specialized is used on a given platform (see https://unix.stackexchange.com/questions/6930/how-is-a-message-queue-implemented-in-the-linux-kernel).

A quick scan of the source code confirms a straight-forward implementation on first principles using the building blocks from the library:

//Mutex to protect data structures
interprocess_mutex         m_mutex;
//Condition block receivers when there are no messages
interprocess_condition     m_cond_recv;
//Condition block senders when the queue is full
interprocess_condition     m_cond_send;
#if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
//Current start offset in the circular index
size_type                  m_cur_first_msg;
size_type                  m_blocked_senders;
size_type                  m_blocked_receivers;
#endif

There's extensive inline documentation. I suggest you read through it if you want to know more.



来源:https://stackoverflow.com/questions/63135687/boostinterprocess-message-queue-timed-receive-internal-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!