本文介绍了boost ::进程间消息队列timed_receive()内部过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

已经谢谢你了.

推荐答案

该库无需记录其工作方式,因为这是实现细节.理想情况下,您无需知道,这就是为什么首先使用库的原因.

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:

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

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.

这篇关于boost ::进程间消息队列timed_receive()内部过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:34