[zeromq-dev] zmq_poll: timeout issue
Ilja Golshtein
ilejn at yandex.ru
Tue Aug 10 18:46:10 CEST 2010
Martin, Matt,
thank you very much for clarification
and for speedy answering.
To be honest I don't fill why spurious wakeups are unavoidable in 0mq ... though it doesn't matter.
Here is a mockup of client application
==
// One I/O thread in the thread pool will do.
zmq::context_t ctx (1);
std::auto_ptr<zmq::socket_t> sp;
sp.reset(new zmq::socket_t(ctx, ZMQ_REQ/*ZMQ_XREQ*/));
// Connect to the server.
sp->connect ("tcp://localhost:23001");
for (int i = 0; i != 20; i++)
{
zmq::message_t request (10);
strcpy((char*)request.data (), "AAAAAAAAA");
sp->send (request);
std::cout << "sent" << std::endl;
zmq_pollitem_t items[1];
items[0].socket = *sp.get();;
items[0].events = ZMQ_POLLIN;
struct timespec tp1;
::clock_gettime(CLOCK_MONOTONIC, &tp1);
for (;;)
{
int poll_ret = zmq::poll(items, 1, 3000000);
if (items[0].revents == ZMQ_POLLIN)
{
assert(poll_ret > 0);
// Get the reply.
zmq::message_t reply;
sp->recv(&reply);
std::cout << (char*)reply.data() << std::endl;
break;
}
else
{
std::cout << "no event fired" <<
"poll_ret=" << poll_ret << std::endl;
struct timespec tp2;
::clock_gettime(CLOCK_MONOTONIC, &tp2);
if (tp2.tv_sec - tp1.tv_sec < 3)
{
continue;
}
else
{
std::cout << "reconnecting - message lost" << std::endl;
sp.reset(new zmq::socket_t(ctx, ZMQ_REQ));
sp->connect ("tcp://localhost:23001");
break;
}
}
}
}
==
The goals are
1. Don't got stuck if response is lost
2. Not block if server unavailable.
The only reason of recreating socket is REQ/REP paradigm (perhaps XREQ/XREP are more suitable).
Suggestions are appreciated.
Thanks.
10.08.2010, 18:07, "Matt Weinstein" <matt_weinstein at yahoo.com>:
> This is the same problem as pthread condition variables have, e.g.:
>
> http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html
>
> Just think of the revents as condition variables to check...
>
> Does someone want to write a quick wrapper ?
>
> PS I recommend CLOCK_MONOTONIC, but it may not be available, and
> requires -lrt ...
>
--
Best regards
Ilja Golshtein
More information about the zeromq-dev
mailing list