[zeromq-dev] a question about implementing an asynchronous reply server

kiddywan kiddywan at 163.com
Fri Aug 19 07:58:58 CEST 2011


Hi,
I have a question about implementing an asynchronous reply server. From the examples, I have to create some worker threads in advance. Can I create the thread only when necessay? How about the following code on Windows?
 
 
static void* worker_routine (void *context)
{
    int rc;
    // Socket to talk to dispatcher
    void *receiver = zmq_socket (context, ZMQ_REP);
    rc = zmq_connect (receiver, "inproc://workers");
    assert(rc == 0);
    char *string = s_recv (receiver);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)worker_routine, context, 0, NULL);
    free (string);
    // Do some 'work'
    Sleep (1000);
   
    // Send reply back to client
    s_send (receiver, "World");
    zmq_close (receiver);
    return NULL;
}
 
void Service()
{
    void *context = zmq_init (1);
    // Socket to talk to clients
    void *clients = zmq_socket (context, ZMQ_ROUTER);
    zmq_bind (clients, "tcp://*:5559");
    // Socket to talk to workers
    void *workers = zmq_socket (context, ZMQ_DEALER);
    zmq_bind (workers, "inproc://workers");
    // Create one worker thread at first.
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)worker_routine, context, 0, NULL);
    // Connect work threads to client threads via a queue
    zmq_device (ZMQ_QUEUE, clients, workers);
    // We never get here but clean up anyhow
    zmq_close (clients);
    zmq_close (workers);
    zmq_term (context);
}
int _tmain(int argc, _TCHAR* argv[])
{
    Service();
    return 0;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110819/0a4d5923/attachment.htm>


More information about the zeromq-dev mailing list