[zeromq-dev] full duplex asynch messaging

Martin Sustrik sustrik at 250bpm.com
Mon Mar 15 14:08:19 CET 2010


Arun,

>     Thanks for you quick response. In my application there can be 
> messages to be sent and received at random unpredictable points of time 
> (and in any order) and they need to be processed with minimal latency. 
>  The latter would require me to call recv in a loop. The sends would 
> come in from a separate thread. From docs, i see that i can only access 
> zmq_socket from the creating thread. So this essentially needs the 
> sender thread to enqueue its messages somewhere , then the looping 
> thread has to repeatedly initiate non blocking recvs and chk send queue, 
> then a (nonblocking) send  etc within a single loop. That doesnt look 
> like a very efficient design at the surface. I was ideally looking for 
> something which fits the below model.  Is there a callback which can be 
> registered when there are messages waiting to be read ? If not, do you 
> have any suggestions for the above requirements?
>   
> // This method would be called by one single thread
> sendMessage(msg)
> {
>      socket.send(msg);
> }
> 
> // incoming message from network should 
> // call below method (from a specifed thread or pool)
> // for boost asio , this is the thread/s which start io_service
> messageFromNetworkCallback(msg)
> {
>      process_message (msg);
> }

If you like the callback API better than socket API, create a thread 
that would do following:

void recv_thread_routine ()
{
     socket_t s (ctx, ZMQ_SUB);
     s.connect (...);

     while (true) {
         message_t msg;
         s.recv (&msg);
         callback (msg);
     }
}

Martin



More information about the zeromq-dev mailing list