boost::interprocess消息队列timed_receive()内部过程

boost::interprocess message queue timed_receive() internal procedure

我目前正在使用 boost::interprocess 库中的 timed_receive() 方法来接收数据。由于接收消息的时间会有所不同,因此我在 receive() 方法上使用了此方法。

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

问题: 此方法如何知道缓冲区中存在消息?它是轮询机制还是实施了更复杂的机制? 我阅读了文档,但找不到任何详细信息,而且源代码也没有提供任何信息。

已经谢谢了。

库不需要记录它是如何工作的,因为那是一个实现细节。理想情况下,您不需要知道,这就是您首先使用库的原因。

您可以期待库以更原始的库构建块来实现它:

这意味着该条件由另一个进程发出信号。

然而,也有可能在给定平台上使用了更高级/更专业的东西(参见 https://unix.stackexchange.com/questions/6930/how-is-a-message-queue-implemented-in-the-linux-kernel)。

快速浏览源代码可确认使用库中的构建块直接实现了第一性原理:

//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

大量 内联文档。如果你想了解更多,我建议你通读一下。