我正在尝试zeroMQ
用作在多个线程之间实现消息传递系统的一种方式。我尝试了下面的代码,但它不起作用;具体来说,每个线程中的调用zmq_recv
不会等待/阻止任何消息的执行。
你能帮我处理这段代码吗?
我正在使用 Linux 操作系统和 gcc
此致
AFG
static void *
worker_routine (void *context) {
// Socket to talk to dispatcher
void *receiver = zmq_socket (context, ZMQ_REP);
zmq_connect (receiver, "inproc://workers");
while (1) {
zmq_msg_t request;
zmq_msg_init( &request );
zmq_recv( receiver, &request, 0 );
printf ("Received request\n");
// Do some 'work'
usleep (1000);
// Send reply back to client
zmq_send (receiver, &request, 0);
}
zmq_close (receiver);
return NULL;
}
int main (void) {
void *context = zmq_init (1);
void *clients = zmq_socket (context, ZMQ_REP);
zmq_bind (clients, "inproc://workers");
int thread_nbr;
for (thread_nbr = 0; thread_nbr < 5; thread_nbr++) {
pthread_t worker;
pthread_create (&worker, NULL, worker_routine, context);
}
zmq_close (clients);
zmq_term (context);
return 0;
}