2

Refer to http://hintjens.wdfiles.com/local--files/main:files/cc1pe.pdf page 67.

Question> I don't see why the following code is good for multi-thread. Based on my understanding, the main thread can do different things while waiting for the feedback from child thread.

However, in the following code, it seems that the step2 is blocked while calling char *string = s_recv (receiver); and step3 is blocked while calling char *string = s_recv (receiver);

Based on my understanding, (step_i+1) is able to do something free until the signal is received from step_i. however, as you can see the code, both the step2 and step3 are blocked and cannot do anything. Why this is a multi-thread code?

Thank you

//  Multithreaded relay

#include "zhelpers.h"
#include <pthread.h>

static void *
step1 (void *context) {
    //  Connect to step2 and tell it we're ready
    void *xmitter = zmq_socket (context, ZMQ_PAIR);
    zmq_connect (xmitter, "inproc://step2");
    printf ("Step 1 ready, signaling step 2\n");
    s_send (xmitter, "READY");
    zmq_close (xmitter);

    return NULL;
}

static void *
step2 (void *context) {
    //  Bind inproc socket before starting step1
    void *receiver = zmq_socket (context, ZMQ_PAIR);
    zmq_bind (receiver, "inproc://step2");
    pthread_t thread;
    pthread_create (&thread, NULL, step1, context);

    //  Wait for signal and pass it on
    char *string = s_recv (receiver);
    free (string);
    zmq_close (receiver);

    //  Connect to step3 and tell it we're ready
    void *xmitter = zmq_socket (context, ZMQ_PAIR);
    zmq_connect (xmitter, "inproc://step3");
    printf ("Step 2 ready, signaling step 3\n");
    s_send (xmitter, "READY");
    zmq_close (xmitter);

    return NULL;
}

int main (void)
{
    void *context = zmq_ctx_new ();

    //  Bind inproc socket before starting step2
    void *receiver = zmq_socket (context, ZMQ_PAIR);
    zmq_bind (receiver, "inproc://step3");
    pthread_t thread;
    pthread_create (&thread, NULL, step2, context);

    //  Wait for signal
    char *string = s_recv (receiver);
    free (string);
    zmq_close (receiver);

    printf ("Test successful!\n");
    zmq_ctx_destroy (context);
    return 0;
}

1 Answer 1

2

The way I read it, the sample code shows only how to let the main thread (step3) know that the other two threads have been created and connected properly. The signal from step1 passes through step2 to step3 so once it arrives there all is set up and ready to go. This ought to happen quite quickly.

When the threads are done, instead of joining them, you could pass a new message around that says DONE. Step 1 sends this message once it's done. Each other step starts waiting for this message once it's done and when it receives one, passes it on to the next step. When step3 receives the DONE message, all threads are done.

Or you could use such message for accumulating the results of each thread's work.

Instead of waiting on an object, you start a synchronous receive of a message. Instead of notifying, you send a message.

So if I understand it right, the multithreaded work is supposed to happen in between these synchronisation messages.


EDIT

it seems that the step2 is blocked while calling s_recv

This is fine in this example, cause step2 has nothing useful to do other than waiting for the READY message. If in your use case a thread has to do work AND regularly check if it has a message waiting for it, it can call zmq_recv in non-blocking mode with the ZMQ_DONTWAIT flag, to check if the message has arrived yet.

3
  • 1
    Yes, we use messages to synchronize between threads since its a pattern that works no matter whether your tasks are threads, processes, or different boxes. I.e. you can wait on N tasks, some of which are other threads, some other processes, some on the other side of the world, using the exact same code and semantics. Apr 25, 2013 at 18:22
  • Your comment simply reiterates what I have understood. I think a concrete example will solve my issue. Let assume that step1 takes 1 minute to finish and step2 takes extra 2 minutes. While waiting for the other thread, step2 will keep printing "Hello World" once per second and step3 will keep printing "Apple" one per 10 seconds. When step2 receives the signal from step1, it stops printing and sends a signal to step3. When step3 receives the signal from step2, it stops printing. How can I implement this with ZeroMQ?
    – q0987
    Apr 25, 2013 at 23:54
  • 1
    In between printing its "Hello World"s, step2 can do a zmq_recv in non-blocking mode with the ZMQ_DONTWAIT flag, to check if the message from step1 has arrived yet.
    – flup
    Apr 26, 2013 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.