In Code Connected book volume 1(page 23) there is a example for using PUSH, PULL messaging. Before closing sockets it uses sleep(). Here is the code:
printf("Total expected cost: %d msec\n", total_msec);
sleep(1); // Give 0MQ time to deliver
zmq_close(sink);
zmq_close(sender);
zmq_ctx_destroy(context);
What is that sleep(1) about? Is this general rule?
The sleep (1) in taskvent.c and tasksink2.c were hangovers from when the examples still used 0MQ/2.2, and you can delete these two lines of code, if you’re running on 0MQ/3.2. I’ve just done that, tested, and it works as you’d expect.
The reason: in 2.2, sockets were destroyed and messages discarded, when you terminated the context. In 3.2, messages will be delivered within a timeout specified by the LINGER socket option, which is by default infinite.
There are lots of other examples that use “sleep”, for good reasons:
You can do start-up and shutdown synchronization differently but it gets more complex than we want in simple examples.