简体   繁体   中英

Trying to exit from a blocking UDP socket read

This is a question similar to Proper way to close a blocking UDP socket . I have a thread in C which is reading from a UDP socket. The read is blocking. I would like to know if it is possible to be able to exit the thread, without relying on the recv() returning? For example can I close the socket from another thread and safely expect the socket read thread to exit? Didn't see any high voted answer on that thread, thats why I am asking it again.

This really depends on what system you're running under. For example, if you're running under a POSIX-compliant system and your thread is cancelable, the recv() call will be interrupted when you cancel the thread since it's a cancel point.

If you're using an older socket implementation, you could set a signal handler for your thread for something like SIGUSR1 and hope nobody else wanted it and signal, since recv() will interrupt on a signal. Your best option is not to block, if at all possible.

I don't think closing a socket involved in a blocking operation is a safe guaranteed way of terminating the operation. For instance, kernel.org warns darkly:

It is probably unwise to close file descriptors while they may be in use by system calls in other threads in the same process. Since a file descriptor may be reused, there are some obscure race conditions that may cause unintended side effects.

  • Instead you could use a signal and make recv fail with EINTR (make sure SA_RESTART is not enabled). You can send a signal to a specific thread with pthread_kill

  • You could enable SO_RCVTIMEO on the socket before starting the recv call

Personally I usually try to stay clear of all the signal nastiness but it's a viable option.

You've got a couple of options for that. A signal will interrupt the read operation, so all you need to do is make sure a signal goes off. The recv operation should fail with error number EINTR.

The simplest option is to set up a timer to interrupt your own process after some timeout eg 30 seconds:

itimerval timer
timeval time;
time.tv_sec = 30;
time.tv_usec = 0;
timer.it_value = time;
if( setitimer( ITIMER_REAL, &timer, NULL ) != 0 )
  printf( "failed to start timer\n" );

You'll get a SIGALRM after the specified time, which will interrupt your blocking operation, and give you the chance to repeat the operation or quit.

You cannot deallocate a shared resource while another thread is or might be using it. In practice, you will find that you cannot even write code to do what you suggest.

Think about it. When you go to call close , how can you possibly know that the other thread is actually blocked in recv ? What if it's about to call recv , but then another thread calls socket and gets the descriptor you just closed? Now, not only will that thread not detect any error, but it will be calling recv on the wrong socket!

There is probably a good way to solve your outer problem, the reason you need to exit from a blocking UDP socket read. There are also several ugly hacks available. The basic approach is to make the socket non-blocking and instead of making a blocking UDP socket read, fake a blocking read with select or poll . You can then abort this loop several ways:

One way is to have select time out and check an 'abort' flag when select returns.

Another way is to also select on the read end of a pipe. Send a single byte to the pipe to abort the select .

If posix complient system, you can try to monitor your thread: with a function that makes your and just after, then returns. ,其功能使你的其后,然后返回。
The calling thread makes a with the desired timeout and terminates the called thread if timed_out. ,并在timed_out时终止被调用的线程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM