c++ - boost::进程间消息队列timed_receive()内部过程

标签 c++ boost message-queue

我目前使用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))
问题:
此方法如何知道缓冲区中存在消息?是轮询机制还是实现了更复杂的机制?
我阅读了文档,找不到任何详细信息,源代码也没有提供任何信息。
谢谢了

最佳答案

该库不需要记录其工作方式,因为这是实现细节。理想情况下,您无需知道,这就是为什么首先使用库的原因。
您可以期望该库以更多原始库构建块的形式实现它:

  • shared_memory_object +映射的区域
  • mutex,+ condition

  • 这意味着该条件由另一个进程发出信号。
    但是,也有可能在给定的平台上使用了更高级的/专门的功能(请参阅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
    
    广泛的内联文档。如果您想了解更多信息,建议您通读它。

    关于c++ - boost::进程间消息队列timed_receive()内部过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63135687/

    相关文章:

    java - 如何使用Spring Integration和MongoDB实现消息队列?

    c++ - 为什么允许此举?

    c++ - boost::variant将static_visitor应用于某些类型

    c++ - 什么消耗更多存储空间 - boost::dynamic_bitset<> 或原始存储?

    multithreading - 对具有极高消息速率的应用程序进行多线程处理(我应该使用哪种方法?)

    xml - Spring 集成 : how to process multiple messages at one time?

    c++ - VSC++2010 : -Zm135

    c++ - 这段代码是如何工作的,它叫什么

    c++ - OpenGL/C++ - 生成 UV 坐标

    c++ - 如何处理返回 string_type 的 C++ boost::filesystem 函数?