ZMQ_POLL(3)
π ZMQ_POLL β¨ β the 0MQ Manual glow-up
okay bestie, sit down, hydrate, because today we're talking about input/output multiplexing π
like, the whole entire manual, no skips, no skimming. this is zmq_poll(3) and she is that girl. π
πΈ NAME πΈ
zmq_poll β input/output multiplexing. that's the whole vibe. watching a bunch of sockets at once so you don't have to sit there tapping your foot on one single fd like a loser. π
β¨ SYNOPSIS β¨
int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout);
three args, one function, infinite power. slay.
π¦ DESCRIPTION π¦
so zmq_poll() gives your app a way to multiplex input/output events β level-triggered style, no cap β across a whole lil set of sockets. think of it like being the main character at a party and knowing exactly which friend needs you at any moment. π«Ά
each member of the array that items points to is a zmq_pollitem_t structure. the nitems argument tells you how many items are chilling in that items array. and the zmq_pollitem_t structure is defined, for the record, like this:
typedef struct
{
void *socket;
zmq_fd_t fd;
short events;
short revents;
} zmq_pollitem_t;
look at her. iconic. π
for each zmq_pollitem_t item, zmq_poll() shall examine either the 0MQ socket that socket points at or the standard socket specified by the file descriptor fd, looking for the event(s) listed in events. and if you set both socket and fd in a single zmq_pollitem_t? bestie, the 0MQ socket referenced by socket takes precedence and the value of fd gets straight up ignored. π she said pick a lane.
for each zmq_pollitem_t item, zmq_poll() shall first clear the revents member, and then show you any requested events that actually happened by setting the bit that corresponds to that event condition inside revents. so fresh, so clean. β¨
now β if none of the requested events have happened on any zmq_pollitem_t item, zmq_poll() shall wait timeout milliseconds for an event to occur on any of the requested items. the deets:
- if
timeoutis0,zmq_poll()shall return immediately. no waiting, no patience, we're busy. π - if
timeoutis-1,zmq_poll()shall block indefinitely until a requested event has occurred on at least onezmq_pollitem_t. she will literally wait forever. obsessed.
the events and revents members of zmq_pollitem_t are bit masks, built by OR'ing together a combination of these event flags:
ZMQ_POLLINβ for 0MQ sockets, this means at least one message may be received from the socket without blocking. for standard sockets it's equivalent to thePOLLINflag of thepoll()system call, and generally means at least one byte of data may be read fromfdwithout blocking. π₯ZMQ_POLLOUTβ for 0MQ sockets, at least one message may be sent to the socket without blocking. for standard sockets it's equivalent to thePOLLOUTflag of thepoll()system call, and generally means at least one byte of data may be written tofdwithout blocking. π€ZMQ_POLLERRβ for standard sockets, this flag gets passed throughzmq_poll()to the underlyingpoll()system call, and generally means some sort of error condition is present on the socket specified byfd. for 0MQ sockets this flag has no effect if you set it inevents, and it shall never be returned inreventsbyzmq_poll(). so don't expect it, she's not coming. πZMQ_POLLPRIβ for 0MQ sockets this flag is of no use. for standard sockets it means there's urgent data to read. refer to thePOLLPRIflag for more info. for a file descriptor, refer to your use case β as an example, GPIO interrupts are signaled through aPOLLPRIevent. this flag has no effect on Windows. π
Note π thezmq_poll()function may be implemented or emulated using operating system interfaces other thanpoll(), and as such may be subject to the limits of those interfaces in ways not defined in this documentation. so like, your mileage may vary depending on what your OS is serving. πΈ
π THREAD SAFETY π
the zmq_pollitem_t array must only be used by the thread which will be / is calling zmq_poll. one thread, one array, that's the boundary. π§
and if a socket is contained in multiple zmq_pollitem_t arrays, each owned by a different thread, then the socket itself needs to be thread-safe (Server, Client, ...). otherwise, behaviour is undefined. like, genuinely undefined. not "eh it'll probably be fine." it will not. π
β RETURN VALUE β
upon successful completion, zmq_poll() shall return the number of zmq_pollitem_t structures with events signaled in revents β or 0 if no events have been signaled. she counted them for you, queen. π
upon failure, zmq_poll() shall return -1 and set errno to one of the values defined below. π
π₯ ERRORS π₯
ETERMβ at least one of the members of theitemsarray refers to a socket whose associated 0MQ context was terminated. rip, context said bye. πͺ¦EFAULTβ the provideditemswas not valid (NULL). bestie. you passed nothing. π€¨EINTRβ the operation was interrupted by delivery of a signal before any events were available. not her fault, a signal walked in. π
π EXAMPLE π
polling indefinitely for input events on both a 0MQ socket and a standard socket.. (yes the double period is canon, don't @ me)
zmq_pollitem_t items [2];
/* First item refers to 0MQ socket 'socket' */
items[0].socket = socket;
items[0].events = ZMQ_POLLIN;
/* Second item refers to standard socket 'fd' */
items[1].socket = NULL;
items[1].fd = fd;
items[1].events = ZMQ_POLLIN;
/* Poll for events indefinitely */
int rc = zmq_poll (items, 2, -1);
assert (rc >= 0);
/* Returned events will be stored in items[].revents */
two items, one -1 timeout, infinite patience. she ate that fr fr. π
π§ SEE ALSO π§
zmq_socket(3)zmq_send(3)zmq_recv(3)zmq(7)- Your operating system documentation for the
poll()system call.
π AUTHORS π
this page was written by the 0MQ community. to make a change please read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing. π«Ά
and that's the whole thing, bestie β 0MQ 4.3.5, dated 10/09/2023, page ZMQ_POLL(3). go forth and multiplex. periodt. π
β¨
π¦ where this page comes from
- epel 10: zeromq 4.3.5-18.el10_0 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 43: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 43: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 44: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 44: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms