将类对象指针打包成 char * 用于消息队列

发布于 2024-12-11 10:02:37 字数 310 浏览 0 评论 0原文

是否可以通过 POSIX 消息队列正确且安全地传递类对象指针?

例如,

Object *obj = new Object();

mq_send(mqdes, static_cast<char*>&obj, sizeof(obj), 1);

在接收端,执行 reinterpret_cast 返回到我的 Object

由于消息队列在 Linux 上使用文件描述符,我很好奇它是如何工作的。我尝试过但不成功,但我认为我可能做错了什么。

Is it possible to properly and safely pass a class object pointer through a POSIX message queue?

For instance,

Object *obj = new Object();

mq_send(mqdes, static_cast<char*>&obj, sizeof(obj), 1);

and on the receive side, perform a reinterpret_cast back to my Object?

Since message queues use a file descriptor on Linux, I am curious how this works. I have tried it unsuccessfully but think I may be doing something wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-12-18 10:02:37

并非如此......仅当对象仅包含基本类型字段和具有基本类型字段的其他结构时。如果您发送一个指针,如果它驻留在另一个进程或另一个系统中,则不能从另一端重用它。

还使用带有继承和虚方法的类,可能会一团糟!

从我的角度来看,最好添加一种 Serialize 方法。

此外,以这种方式传递序列化的结构体二进制文件根本不可移植,并且如果您想在其他系统中使用相同的机制或者更改结构或对象打包等内容,可能会给您带来几个问题。

自定义序列化\反序列化将是首选并且更可移植,但选择当然是您的。

就像...

template<typename T>
int SerializeAndSendObject(mqd_t mqdes, const T* instance)
{
    MySerializationStream stream;
    instance->SerializeTo(stream);
    mq_send(stream.toBuffer(), stream.size());
}

如果您只是在两个线程之间发送而不是发送对象的内容,我将只发送指向用 new 分配的对象的指针,然后我将从另一端取消分配它。
请注意,当您处置队列时,您必须首先销毁所有挂起的对象!

Object* pointer = &obj;
mq_send(mqdes, static_cast<char*>(pointer), sizeof(Object*), 1);

请注意 sizeof(Object*)...您只需发送指针,而不是对象本身。

Not really... only if the object contains only basic type fields and other structs with basic type fields. If you send a pointer it cannot be reused from the other side if it resides in another process or in another system.

Also using classes with inheritance and virtual methods, it may be a mess!

It is better to add a sort of Serialize method from my point of view.

Also passing a struct binary serialized in this way is not portable at all and can bring you to several problems if you want to use the same mechanism with other systems or if you change the structure or things like the packing of the object.

A custom serialization\deserialization would be preferred and more portable, but the choice is of course your.

Something like ...

template<typename T>
int SerializeAndSendObject(mqd_t mqdes, const T* instance)
{
    MySerializationStream stream;
    instance->SerializeTo(stream);
    mq_send(stream.toBuffer(), stream.size());
}

If you are just sending between two threads instead of sending the content of the object i would send just the pointer to an object allocated with new and i would deallocate it from the other side.
Be careful that when you dispose the queue you must have first destroyed all pending objects!

Object* pointer = &obj;
mq_send(mqdes, static_cast<char*>(pointer), sizeof(Object*), 1);

Note the sizeof(Object*)... you need to send only the pointer, not the object itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文