I started to use ZeroMQ for IPC and made a simple echo-client/server and I'm surprised about one thing. Here is the C++ code (using zmq.hpp
and zmq_addon.hpp
).
Server:
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("ipc:///tmp/machine-1");
while (1) {
zmq::multipart_t m;
m.recv(socket);
int i = m.poptyp<int>();
i++;
m.addtyp<int>(i);
m.send(socket);
}
Client:
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REQ);
socket.connect("ipc:///tmp/machine-1");
int i = 0;
while (1) {
int save = i;
zmq::multipart_t m;
m.addtyp<int>(i);
m.send(socket);
m.recv(socket);
i = m.poptyp<int>();
if (i != (save + 1))
break;
if ((i % 100000) == 0)
std::cerr << "i : " << i<< "\n";
}
I works as expected. The client is sending an int
, the server does plus one and sends it back.
Now the magic I don't understand: I realized, that I can run the client several times in parallel and it continues to works, for each client correctly.
The check comparing save+1
to i
is always OK.
How does ZMQ handles the concurrency problem on the server side? How does it know to which client the response has to be send back?
There is this question on SO, but it doesn't answer my question: ZeroMQ REQ/REP on ipc:// and concurrency
receive
andsend
takes long). At least not with this basic mechanism – Orchestral