When I used zeromq-2.1.7 version, everything was fine. I create a listening
server.
But My cpu was occupied about 50%.
I tracked the source code.
I found prior version that the socket is in blocking mode.
mailbox.cpp
int zmq::mailbox_t::recv (command_t *cmd_, bool block_)
{ //other code
int nbytes = ::recv (r, (char *)cmd_, sizeof (command_t), 0);
//other code
}
The next version that the code is in "while" circuit whick make program
constantly loop, it ate my resource.:p
socket_base.cpp
int zmq::socket_base_t::recv (msg_t *msg_, int flags_)
{
//other code
while (true) {
if (unlikely (process_commands (block ? timeout : 0, false) != 0))
return -1;
rc = xrecv (msg_, flags_);
if (rc == 0) {
ticks = 0;
break;
}
if (unlikely (errno != EAGAIN))
return -1;
block = true;
if (timeout > 0) {
timeout = (int) (end - clock.now_ms ());
if (timeout <= 0) {
errno = EAGAIN;
return -1;
}
}
}
//other code
}
example:
int main (void)
{
void *context = zmq_init (1);
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
int i = zmq_bind (responder, "tcp://*:5555");
printf ("bind done %d" , i);
while (1) {
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
zmq_recv (responder, &request, 0);//version 3.0.0 use
zmq_recvmsg(responder, &request, 0)
printf ("Received Hello\n");
zmq_msg_close (&request);
// Do some 'work'
Sleep (1);
// Send reply back to client
zmq_msg_t reply;
zmq_msg_init_size (&reply, 5);
memcpy (zmq_msg_data (&reply), "World", 5);
zmq_send (responder, &reply, 0);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_term (context);
return 0;
}
Can u suggest me the right way to do so?
Best regards.
Thanks.
suncan
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev