[zeromq-dev] One socket per thread or per call?

johan mabille johan.mabille at gmail.com
Tue Jan 31 13:10:51 CET 2017


Actually, the cpp bindings closes everything (the functions of libzmq for
closing sockets are called inside the destructor of socket_t).


On Tue, Jan 31, 2017 at 12:43 PM, Luca Boccassi <luca.boccassi at gmail.com>
wrote:

> On Mon, 2017-01-30 at 23:22 +0100, Patrick Boettcher wrote:
> > Hi all,
> >
> > I'm new to zmq and still discovering things and I'd like your feedback
> > on a design decision.
> >
> > (I asked the same question on StackOverflow, before seeing that there
> > is a zmq-mailing list.
> > http://stackoverflow.com/questions/41941702/one-zeromq-
> socket-per-thread-or-per-call)
> >
> > As we all know, a ZeroMQ socket shall not be shared between application
> > threads. Contexts however can.
> >
> > I have a multi-threaded-application and I'd like to have each thread
> > exchange messages from time to time with a REP-socket (event, exceptions
> > and the like) depending what they are doing (they are doing
> > non-ZeroMQ-stuff).
> >
> > To send messages to my REP-socket I use the following function
> > (half-C++ half-pseudo-code):
> >
> >   bool sendMessage(std::string s)
> >   {
> >       zmq::socket_t socket(globalContext(), ZMQ_REQ);
> >       socket.connect(ETT::IPC::httpV1ConcentratorEndpoint);
> >
> >       zmq::message_t message(s.size());
> >       memcpy(message.data(), s.data(), s.size());
> >       if (!socket.send(message))
> >           return false;
> >
> >       // poll on socket for POLLIN with timeout
> >
> >       socket.recv(&message);
> >       // do something with message
> >
> >       return true;
> >   }
> >
> > This function is called from every thread when needed. It creates a
> > local socket, connects, sends the message, and receives a response.
> > At exit, the socket is disconnected and removed (at least I'm
> > assuming that it is closed).
>
> That's the C++ bindings so not sure, but libzmq does not close anything
> for you, you have to do it explicitly
>
> > This way, I don't need to bother to maintain a socket in each of my
> > threads. This comes at the cost of creating and connecting each time I
> > call this function.
> >
> > I stressed this code and I don't see much difference between reusing
> > one socket and this reconnect-implementation. (I have 20k REP-REQ per
> > second, including json-decode/encode, in both cases)
> >
> > Is there a more correct ZeroMQ-way of doing this?
> >
> > best regards,
> > --
> > Patrick.
>
> Closing and reopening sockets continuosly is an anti-pattern, I would
> recommend opening a socket when the thread starts and closing it when
> it's torn down
>
> Kind regards,
> Luca Boccassi
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20170131/57c69370/attachment.htm>


More information about the zeromq-dev mailing list