I wanted to add a ZeroMQ socket to a glib program.
The pitty is, a zmq socket is not poll()-able, and they give their implementation, which overloads the old poll() func. How could I integrate that into the main loop efficiently? I tried using their poll ( zmq_poll() ) instead of the default one, but there’s no good way of giving it the zmq socket, besides from making it a global.
Defining a new GSource works, but it can get high CPU usage ( by setting timeout = 0 ) or arbitrary poll timeouts ( e.g. setting timeout = 100 to be polled at least every 100 ms ), which is not really efficient, since there is the possibility of polling.
I found that newer zmq libraries support the
ZMQ_FDgetsockopt()parameter, which gives you back a unixfdwhich you canpoll(). The only caveat is that you can’t justpoll()it to know if you canrecv()orsend()from / to it, but you need to use theZMQ_EVENTSgetsockopt()parameter to get back the realfdstatus.It seems to be working quite well in glib.