c++ - How can I clean up properly when recv is blocking? -


consider example code below (i typed example, if there errors doesn't matter - i'm interested in theory).

bool shutdown = false; //global  int main() {   createthread(null, 0, &messengerloop, null, 0, null);   //do other programmy stuff... }   dword winapi messengerloop( lpvoid lpparam ) {   zmq::context_t context(1);   zmq::socket_t socket (context, zmq_sub);   socket.connect("tcp://localhost:5556");   socket.setsockopt(zmq_subscribe, "10001 ", 6);    while(!shutdown)   {     zmq_msg_t getmessage;     zmq_msg_init(&getmessage);     zmq_msg_recv (&getmessage, socket, 0); //this line wait forever message     processmessage(getmessage);    } } 

a thread created wait incoming messages , handle them appropriately. thread looping until shutdown set true.

in zeromq guide states must cleaned up, namely messages, socket , context.

my issue is: since recv wait forever message, blocking thread, how can shut down thread safely if message never received?

the blocking call exit in few ways. first, , depends on language , binding, interrupt (ctrl-c, sigint, sigterm) exit call. you'll (again, depending on binding) error or null message (libzmq returns eintr error).

second, if terminate context in thread, blocking call exit (libzmq returns eterm error).

thirdly, can set timeouts on socket return in case after timeout, if there's no data. don't can useful in cases.

finally, in practice never blocking receives use zmq_poll find out when sockets have messages waiting, receive sockets. how scale out handling more sockets.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -