ZeroMQ: how to achieve C-like multithreading

In C, we have Sockets and descriptors, you can just take one of them and pass them to Thread, this allows you to accept incoming connections and give Thread work that you like, and Thread can send the answer itself.

My question is: how can I achieve this with ZeroMQ? With the Request-Reply template, it seems that I cannot send and receive asynchronously, the responses should be consistent, my goal would be to have multiple clients on the same server, sending the replies out of sequence.

I looked at the Request Response template, but the API clearly states that using this Socket with multiple Threads is a bad idea. Maybe I missed something, or ZeroMQ is smarter than I know. If you need more information, just send a comment and I will do my best to provide the information.

I also looked at the examples below: Code examples

Here is the description of the Socket: ZMQ-Socket

+6
source share
3 answers

After several days of searching, a friendly person on IRC in the zeromq channel helped me.

Updated link in case someone is still looking for this. Must be stubborn! http://zguide.zeromq.org/

This example really works well, it is easy to adapt, I used boost threads using threadpool.

+3
source

As I understand it, you need a server that wants to create a new thread for each incoming connection, and each thread answers exactly one connection, if this is the example code that does just that: http://www.kieser.net/linux/java_server. html

+2
source

Often when you try to adapt existing projects to 0MQ, you get into trouble. Here, an existing project passes sockets (typically HTTP) to child processes or threads, and then allows them to respond. This is not a very elegant design. It works because every new connection exists as a socket, but because when a response is sent, the socket is destroyed. None of these apply to 0MQ applications.

The 0MQ design uses an external ROUTER interface that handles customer requests. He then passes these requests to worker threads through inproc: // sockets, using the backend DEALER to process requests. Worker threads use the REP socket to receive requests and send their responses back. The main thread is then polled both in the interface and in the backend socket, simply by routing messages between them.

Here's what this old blog article explains (using legacy XREP / XREQ names), and it is explained in more detail by examples in many languages ​​here: http://zguide.zeromq.org/page:all#Multithreading-with-MQ

+2
source

Source: https://habr.com/ru/post/919010/


All Articles