im当前使用 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))
问题: 此方法如何知道缓冲区中存在消息?是轮询机制还是实现了更复杂的机制? 我阅读了文档,找不到任何详细信息,源代码也没有提供任何信息。
已经感谢。
答案 0 :(得分:1)
该库无需记录其工作方式,因为这是实现细节。理想情况下,您无需知道,这就是为什么首先使用库的原因。
您可以期望库以更多原始库构建块的形式实现它:
这意味着该条件由另一个进程发出信号。
但是,也有可能在给定的平台上使用了更高级的/专门的功能(请参见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
有广泛的内联文档。如果您想了解更多信息,建议您通读它。