简体   繁体   English

订阅者上的 ZMQ recv 不遵守设置超时

[英]ZMQ recv on subscriber not respecting set timeout

I am trying to integrate ZMQ communication into a test case but I am having issues with zmq::socket_t::recv essentially blocking indefinitely even though I set a timeout 500ms for the operation.我正在尝试将 ZMQ 通信集成到测试用例中,但我遇到了zmq::socket_t::recv的问题,即使我为操作设置了 500 毫秒的超时时间,它也基本上无限期地阻塞。 The minimal test case that exhibits the behavior looks like this展示该行为的最小测试用例如下所示

#include <gtest/gtest.h>
#include <zmq.hpp>

TEST(timeout, not_working_in_recv)
{
  zmq::context_t zmq_context;
  zmq::socket_t  sub(zmq_context, ZMQ_SUB);

  sub.setsockopt(ZMQ_SUBSCRIBE, nullptr, 0);
  int const timeout_ms = 500;
  sub.setsockopt(ZMQ_RCVTIMEO, &timeout_ms);
  sub.connect("inproc://pubsub_test");

  zmq::message_t msg;
  sub.recv(&msg);
}

There have been more elaborate setups where I set up a thread that creates a corresponding publisher, but the result was always a blocking receive.有更精细的设置,我设置了一个创建相应发布者的线程,但结果始终是阻塞接收。 The block is not quite indefinite, eventually it does actually return.该块不是完全不确定的,最终它确实会返回。

在此处输入图像描述

This is compiled and executed under Windows 10, x32 using MSVC 2017 and ZMQ 4.2.2.这是使用 MSVC 2017 和 ZMQ 4.2.2 在 Windows 10、x32 下编译和执行的。

Does anyone know why the receive call is blocking and how this can be fixed?有谁知道为什么接收呼叫被阻止以及如何解决这个问题?

The timeout is set incorrectly.超时设置不正确。 The line线

sub.setsockopt(ZMQ_RCVTIMEO, &timeout_ms); // set timeout to address of timeout_ms variable

should be应该

sub.setsockopt(ZMQ_RCVTIMEO, timeout_ms); // set timeout to value of timeout_ms 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM