[zeromq-dev] thread safety using inproc sockets for inter-thread communication

Bill Torpey wallstprog at gmail.com
Tue Sep 26 23:50:49 CEST 2017


btw, disregard that CALL_ZMQ_FUNC business — that’s just a helper macro that checks rc etc.

  void* temp = zmq_socket(impl->mZmqContext, ZMQ_PUB);
  zmq_connect(temp, ZMQ_CONTROL_ENDPOINT);
  zmq_pollitem_t pollitems [] = { { temp, 0, ZMQ_POLLIN, 0 } };
  zmq_poll(pollitems, 1, 1);				// see https://github.com/zeromq/libzmq/issues/2267
  zmq_send(temp, msg, msgSize, 0);
  zmq_disconnect(temp, ZMQ_CONTROL_ENDPOINT);
  zmq_close(temp);


> On Sep 26, 2017, at 5:22 PM, Bill Torpey <wallstprog at gmail.com> wrote:
> 
> Hi All:
> 
> I don’t see this addressed specifically in the docs, in the GitHub issues, or here, so thought I’d ask.
> 
> I bind an inproc sub socket for inter-thread communication, which I include in zmq_poll.  When I want to e.g., subscribe from a different thread I create a pub socket, connect to the first and send it a command message, which then gets executed on the main zmq thread.  So far, so good.
> 
> What I’m wondering if it’s OK to immediately disconnect the socket after calling zmq_send, like so:
> 
>   void* temp = zmq_socket(impl->mZmqContext, ZMQ_PUB);
>   zmq_connect(temp, ZMQ_CONTROL_ENDPOINT);
>   zmq_pollitem_t pollitems [] = { { temp, 0, ZMQ_POLLIN, 0 } };
>   CALL_ZMQ_FUNC(zmq_poll(pollitems, 1, 1));				// see https://github.com/zeromq/libzmq/issues/2267
>   zmq_send(temp, msg, msgSize, 0);
>   zmq_disconnect(temp, ZMQ_CONTROL_ENDPOINT);
>   zmq_close(temp);
> 
> This *seems* to work, but I’m concerned that maybe I’m just getting "lucky”?  It does *not* appear to work with ZMQ_PAIR sockets, for whatever reason, although I’ve seen that approach recommended in a few places. 
> 
> Thanks in advance for any assistance!
> 
> Regards,
> 
> Bill Torpey




More information about the zeromq-dev mailing list