[zeromq-dev] Is there a way to avoid sleep operation before sending messages ?

Teng Zhang tengz at seas.upenn.edu
Sun May 15 23:49:44 CEST 2016

Hi, I am now working on a simple case where a multi-threaded program tries to send a message to a proxy. The program is written in C and the czmq-3.0.2_3 is used. The platform is Mac OS X.

Here is the the code fragment of the sender:

  publisher = zsock_new_pub (">tcp://localhost:5559");
 zsock_resolve (publisher);
 str = "abc";
 zstr_send (publisher, str);

Here is the code fragment of the proxy:

 bool verbose = true;

    // Create and configure our proxy
 zactor_t *proxy = zactor_new (zproxy, NULL);
 assert (proxy);
 if (verbose) {
     zstr_sendx (proxy, "VERBOSE", NULL);
     zsock_wait (proxy);
    zstr_sendx (proxy, "FRONTEND", "XSUB", "tcp://*:5559", NULL);
    zsock_wait (proxy);
    zstr_sendx (proxy, "BACKEND", "XPUB", "tcp://*:5560", NULL);
    zsock_wait (proxy);

    // Test capture functionality
    zsock_t *capture = zsock_new_pull ("inproc://capture");
    assert (capture);

    // Switch on capturing, check that it works
    zstr_sendx (proxy, "CAPTURE", "inproc://capture", NULL);
    zsock_wait (proxy);

    // Setup loop for captures
    zloop_t *loop = zloop_new ();
    assert (loop);
    zloop_set_verbose (loop, verbose);

    // Set up reader for captures
    int rc = zloop_reader (loop, capture, capture_event, NULL);
    assert (rc == 0);
    zloop_reader_set_tolerant (loop, capture);
    zloop_start (loop);

capture_event is the call back function:

static int
capture_event (zloop_t *loop, zsock_t *reader, void *arg)
    zmsg_t *msg = zmsg_recv (reader);
    char *msg_str = zmsg_popstr (msg);
    printf ("CAPTURED: %s\n", msg_str);
    free (msg_str);
    zmsg_destroy (&msg);
    return 0;

There is another process that can receive the data from the proxy by the port 5560, which is not shown here. 

The problem is that if there is no sleep operation before zstr_send, the proxy cannot receive and print out the message. I also have tried to use sched_yield() to replace sleep but it does not work. Moreover, if there are more threads of sender, the sleep time needs to be longer to make sure that the proxy can receive all the messages. I am wondering if there is any way not to use sleep to send the message. 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20160515/10f88454/attachment.htm>

More information about the zeromq-dev mailing list