zmq::message_t发送后可以重复使用吗?
Can zmq::message_t's be reused after being sent?
我正在使用 ZeroMQ 来实现玩具通信协议;这是我第一次使用这个 framework/library.
现在,在我的协议中,某一方发送多条连续的消息,所有这些消息都具有相同的大小。所以 - 我想,我会避免重新分配它们,只是尝试用不同的内容重新填充消息数据缓冲区,例如:
zmq::message_t msg { fixed_common_size };
while (some_condition()) {
my_filling_routine(msg.data(), fixed_common_size);
the_socket.send(msg);
}
但是在这个循环的第二次迭代中,我遇到了分段错误; msg.data()
虽然不是 nullptr
。我突然想到,也许 ZeroMQ 以某种方式蚕食了内存,因此我需要写这样的东西:
zmq::message_t msg { fixed_common_size };
char buffer[fixed_common_size];
while (some_condition()) {
my_filling_routine(buffer, fixed_common_size);
msg.rebuild(buffer, fixed_common_size);
the_socket.send(msg);
}
但我确信这会导致取消分配和重新分配。
所以,rebuild()
真的是必需的,还是我的代码中存在一些错误?
注意:我使用的是 Unix 套接字,以防答案以某种方式依赖于此。
不可以,zmq::message_t
发送后不能重复使用。
首先,欢迎来到 ZeroMQ,这是 distributed-system 从业者的一个很酷的地方。
ZeroMQ API 非常明确地警告这些问题。成功调用 zmq_msg_send()
,引用消息负载,并不意味着消息本身已实际发送。
您最好尝试想象调用 只是 "moves" 从您的应用程序代码到 ZeroMQ 引擎的责任在 Context()
-instance ... ).
中实例化的 IOthreads
The zmq_msg_t
structure passed to zmq_msg_send()
is nullified during the call.
下一个更好从不如上文所述以后随时触摸它:o)
分配器的最大 "ecological" 旁路是重新使用 整个 消息,如:
If you want to send the same message to multiple sockets you have to copy it using ( e.g. using zmq_msg_copy()
).
复制时的奖金警告...
再加一个提点:可以抄袭,但以后敢尝试修改...
Avoid modifying message content after a message has been copied with zmq_msg_copy()
, doing so can result in undefined behaviour. If what you need is an actual hard copy, allocate a new message using zmq_msg_init_size()
and copy the message content using memcpy()
.
Never access zmq_msg_t
members directly, instead always use the zmq_msg
-family of functions.
我正在使用 ZeroMQ 来实现玩具通信协议;这是我第一次使用这个 framework/library.
现在,在我的协议中,某一方发送多条连续的消息,所有这些消息都具有相同的大小。所以 - 我想,我会避免重新分配它们,只是尝试用不同的内容重新填充消息数据缓冲区,例如:
zmq::message_t msg { fixed_common_size };
while (some_condition()) {
my_filling_routine(msg.data(), fixed_common_size);
the_socket.send(msg);
}
但是在这个循环的第二次迭代中,我遇到了分段错误; msg.data()
虽然不是 nullptr
。我突然想到,也许 ZeroMQ 以某种方式蚕食了内存,因此我需要写这样的东西:
zmq::message_t msg { fixed_common_size };
char buffer[fixed_common_size];
while (some_condition()) {
my_filling_routine(buffer, fixed_common_size);
msg.rebuild(buffer, fixed_common_size);
the_socket.send(msg);
}
但我确信这会导致取消分配和重新分配。
所以,rebuild()
真的是必需的,还是我的代码中存在一些错误?
注意:我使用的是 Unix 套接字,以防答案以某种方式依赖于此。
不可以,zmq::message_t
发送后不能重复使用。
首先,欢迎来到 ZeroMQ,这是 distributed-system 从业者的一个很酷的地方。
ZeroMQ API 非常明确地警告这些问题。成功调用 zmq_msg_send()
,引用消息负载,并不意味着消息本身已实际发送。
您最好尝试想象调用 只是 "moves" 从您的应用程序代码到 ZeroMQ 引擎的责任在 Context()
-instance ... ).
The
zmq_msg_t
structure passed tozmq_msg_send()
is nullified during the call.
下一个更好从不如上文所述以后随时触摸它:o)
分配器的最大 "ecological" 旁路是重新使用 整个 消息,如:
If you want to send the same message to multiple sockets you have to copy it using ( e.g. using
zmq_msg_copy()
).
复制时的奖金警告...
再加一个提点:可以抄袭,但以后敢尝试修改...
Avoid modifying message content after a message has been copied with
zmq_msg_copy()
, doing so can result in undefined behaviour. If what you need is an actual hard copy, allocate a new message usingzmq_msg_init_size()
and copy the message content usingmemcpy()
.
Never accesszmq_msg_t
members directly, instead always use thezmq_msg
-family of functions.