Skip to content

Instantly share code, notes, and snippets.

@somdoron
Last active May 31, 2017 16:29
Show Gist options
  • Save somdoron/902169bf115d3534bd24 to your computer and use it in GitHub Desktop.
Save somdoron/902169bf115d3534bd24 to your computer and use it in GitHub Desktop.
polling on multiple sockets
void* ctx = zmq_ctx_new();
void* client = zmq_socket(ctx, ZMQ_CLIENT);
// creating new pollfd, pollfd is per thread.
void* pollfd = zmq_pollfd_new();
// associate pollfd with client, you can associate multiple pollfd with one socket for multi threading
zmq_add_pollfd(client, pollfd);
zmq_pollitem_t items[] {
{client, 0, ZMQ_POLLIN, 0}
}
// Calling the zmq_pollfd_poll to poll on thread safe sockets, very similar to zmq_poll and migration should be simple
int rc = zmq_pollfd_poll (pollfd, items, 1,-1);
zmq_msg_t msg;
zmq_msg_init(&msg);
// When polling from multiple threads it is recommended to use dont wait flag as other threads might already receive the message
rc = zmq_msg_recv(&msg, client, ZMQ_DONTWAIT);
rc = zmq_msg_close (&msg);
rc = zmq_remove_pollfd (client, pollfd);
rc = zmq_pollfd_close (pollfd);
rc = zmq_close (client);
rc = zmq_ctx_term (ctx)
ZMQ_EXPORT int zmq_add_pollfd (void *s, void *pollfd);
ZMQ_EXPORT int zmq_remove_pollfd (void *s, void *pollfd);
ZMQ_EXPORT void *zmq_pollfd_new ();
ZMQ_EXPORT int zmq_pollfd_close (void* pollfd);
ZMQ_EXPORT int zmq_pollfd_poll (void *p, zmq_pollitem_t *items, int nitems, long timeout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment