什么时候我应该使用zmq_msg_t



发送和接收消息的函数如下:

int zmq_send (void *socket, void *buf, size_t len, int flags);
int zmq_recv (void *socket, void *buf, size_t len, int flags);

但是,从文档中不清楚何时必须直接使用 zmq_msg_t 或我的自定义数据。那么在哪些情况下我应该考虑使用 zmq_msg_t,在哪些情况下直接发送我的数据?

这两个

函数都将在内部构造一个zmq_msg_t,然后使用该函数的zmq_msg_t变体。这很方便,但如果您想多次发送相同的消息,创建zmq_msg_t并使用这些变体会更有效(更少复制)。

Never TLDR

在如何设置、加载/卸载和使用/销毁酷炫而强大的 ZeroMQ 库服务所依赖的支持性低级数据结构时要相当小心。

否则,最好使用高级配套函数。


不要犹豫,阅读这本书 - 值得花时间

Pieter HINTJENS在第42页第4项中建议:

要释放(而不是销毁)消息,请调用 zmq_msg_close() 。这会删除引用,最终 0MQ 将销毁该消息。

在 API 文档中,您可能会发现更强烈的警告:

The zmq_msg_close() function shall inform the ØMQ infrastructure
that any resources associated with the message object
referenced by msg are no longer required and may be released.
Actual release of resources associated with the message object
shall be postponed by ØMQ until all users of the message or underlying
data buffer have indicated it is no longer required.
Applications should ensure that zmq_msg_close() is called
once a message is no longer required, otherwise memory leaks may occur.

切勿直接访问zmq_msg_t成员,而应始终使用 zmq_msg 系列函数。

Return value:
              zmq_msg_close() function shall return zero if successful.
                              Otherwise it shall return -1 and
                              set errno to one of the values defined below.
Errors
              EFAULT          Invalid message.

最新更新