Reputation: 506
There seem to be two main ways to asynchronously get data from your socket in ZeroMQ:
if(zmq_recv(&msg, ZMQ_NOBLOCK) == 0) { // could return EAGAIN or others
// do stuff
}
...and...
if(zmq_poll(&pollItems[0], num, timeout)) {
if(zmq_poll(&msg) == 0) {
// do stuff
}
}
If I only have one socket that I want to read from, is there a compelling reason I should opt for the version that uses zmq_poll
rather than zmq_recv
with ZMQ_NOBLOCK
?
Upvotes: 1
Views: 2127
Reputation: 229274
You would rarely do a zmq_recv(&msg, ZMQ_NOBLOCK)
without using zmq_poll
Normally you would read in a loop;
while (!done) {
...
zmq_recv(&msg, ZMQ_NOBLOCK);
...
}
This is a busy wait, it uses CPU unnecessarily. zmq_poll()
blocks, and don't consume CPU while it's waiting for something to happen.
If you only have one socket, and don't want to use zmq_poll, you would normally use a blocking zmq_recv
, and set a ZMQ_RCVTIMEO so you don't block forever if something has gone bad at the sending side.
Upvotes: 2