Boost message_queue:只有构造函数让我配置它,没有其他可用的成员函数
这是一个经典的Boost message_queue示例:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { ... };
int main() {
Data data1;
message_queue::remove("message_queue");
message_queue mq(create_only, "message_queue", 100, sizeof(Data));
mq.send(&data1, sizeof(Data), 0);
}
现在我想将mq
message_queue对象作为成员变量放入类中,并具有该类对象的生命周期:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { ... };
class DataManager
{
message_queue mq;
public:
DataManager()
: mq(create_only, "message_queue", 100, sizeof(Data)) // ok
{
mq.Open(create_only, "message_queue", 100, sizeof(Data)); // Open does not exist
}
};
看来我只能初始化< code>mq 对象位于成员初始化列表中,因为 message_queue
不提供成员函数来稍后设置其参数。
我错了吗?还有其他方法吗?
例如,我希望能够让对象使用消息队列,该消息队列的名称作为参数传递给其成员函数之一。
谢谢。
This is a classic Boost message_queue example:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { ... };
int main() {
Data data1;
message_queue::remove("message_queue");
message_queue mq(create_only, "message_queue", 100, sizeof(Data));
mq.send(&data1, sizeof(Data), 0);
}
Now I would like to put the mq
message_queue object inside a class as a member variable, with the lifetime of this class' object:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { ... };
class DataManager
{
message_queue mq;
public:
DataManager()
: mq(create_only, "message_queue", 100, sizeof(Data)) // ok
{
mq.Open(create_only, "message_queue", 100, sizeof(Data)); // Open does not exist
}
};
It seems I can only initialise the mq
object in the member initialisation list, since message_queue
does not provide the member functions to set its parameters at a later time.
Am I wrong? Is there another way to do it?
I would like to be able, e.g., to let an object use a message queue whose name is passed as a parameter to one of its member functions.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
只是为了让您了解至少可以将一些参数传递给类构造函数。
由于消息队列底层使用共享内存,所以我认为大多数参数在构建后就无法更改。
How about this :
Just to give you an idea that at least some parameters can be passed to class constructor.
Since message queue uses shared memory underneath, I think most parameters can not be changed after construction.