[zeromq-dev] Aborting a thread monitoring a socket

Richard_Newton at waters.com Richard_Newton at waters.com
Mon Jul 1 10:45:58 CEST 2013

Hi All,

Say I'm monitoring a socket using zmq_socket_monitor and have a monitor thread blocked on a zmq_recvmsg waiting for socket activity, is there a way to
abort the monitoring and unblock the socket other than closing the entire context?

I've tried calling zmq_socket_monitor again with a NULL address, that stops any more events going to the monitor socket but does not unblock it.

Maybe if we call zmq_socket_monitor on an already monitored socket it could send and event to the already connected monitor socket to indicate the

Full example here:

#include <zmq.h>
#include <assert.h>
#include <cstring>
#include <pthread.h>
#include <unistd.h>

static void *req_socket_monitor (void *ctx)
    void *s = zmq_socket (ctx, ZMQ_PAIR);
    assert (s);

    int rc = zmq_connect (s, "inproc://monitor.req");
    assert (rc == 0);

    zmq_msg_t msg;
    zmq_msg_init (&msg);

    printf("Waiting for socket monitor activitiy\n");

    rc = zmq_recvmsg (s, &msg, 0);
    if (rc == -1 && zmq_errno() == ETERM)
        printf("Monitor aborting\n");
        assert (rc != -1);
        printf("Got activity\n");

    zmq_close (s);
    return NULL;

int main(int argc, char* argv[])
    void* ctx = zmq_ctx_new();
    void* req = zmq_socket(ctx, ZMQ_REQ);

    int rc = zmq_socket_monitor (req, "inproc://monitor.req", ZMQ_EVENT_ALL);
    assert (rc == 0);

    printf("Spawning monitor thread\n");
    pthread_t thread;
    rc = pthread_create (&thread, NULL, req_socket_monitor, ctx);
    assert (rc == 0);


    printf("Aborting monitor thread\n");

    // What to do here to unblock monitor thread?
    rc = zmq_socket_monitor (req, NULL, ZMQ_EVENT_ALL);

    pthread_join(thread, NULL);
    printf("Monitor thread aborted\n");


    return 0;


The information in this email is confidential, and is intended solely for the addressee(s). 
Access to this email by anyone else is unauthorized and therefore prohibited.  If you are 
not the intended recipient you are notified that disclosing, copying, distributing or taking 
any action in reliance on the contents of this information is strictly prohibited and may be unlawful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130701/988f0b57/attachment.htm>

More information about the zeromq-dev mailing list