in my Cocoa project, I communicate with a device connected to a serial port. Now, I am waiting for the serial device to send a particular message of some bytes. For the read operation (and the reaction for once the desired message has been received), I created a new thread. On user request, I want to be able to cancel the thread.
As Apple suggests in the documentos, I added a flag to the thread dictionary, periodically check if the flag has been set and if so, call
[NSThread exit]. Esto funciona bien.
Now, the thread may be stuck waiting for the serial device to finally send the 12 byte message. The read call looks like this:
numBytes = read(fileDescriptor, buffer, 12);
Once the thread starts reading from the device, but no data comes in, I can set the flag to tell the thread to finish, but the thread is not going to read the flag unless it finally received at least 12 bytes of data and continues processing.
Is there a way to kill a thread that currently performs a read operation on a serial device?
Edit for clarification: I do not insist in creating a separate thread for the I/O operations with the serial device. If there is a way to encapsulate the operations such that I am able to "kill" them if the user presses a cancel button, I am perfectly happy. I am developing a Cocoa application for desktop Mac OS X, so no restrictions regarding mobile devices and their capabilities apply. A workaround would be to make the read function return immediately if there are no bytes to read. How can I do this?
preguntado el 27 de agosto de 11 a las 22:08
Set the timeout to (say) half a second and call it in a loop while checking to see if your thread should exit.
Asynchronous thread cancellation is almost always a bad idea. Try to stick with event-driven interfaces (and, if necessary, timeouts).
Esto es exactamente lo que
pthread_cancel interface was designed for. You'll want to wrap the block with
pthread_cleanup_pop in order that you can safely clean up if the thread is cancelled, and also disable cancellation (with
pthread_setcancelstate) in other code that runs in this thread that you don't want to be cancellable. This can be a pain if proper cleanup would involve multiple call frames; it essentially forces you to use
pthread_cleanup_push at every call level and structure your thread code like C++ or Java with
catch style exception handling.
An alternative approach would be to install a signal handler for an otherwise-unused signal (like
SIGUSR1 or one of the realtime signals) without the
SA_RESTART flag, so that it interrupts syscalls with
EINTR. The signal handler itself can be a complete no-op; the only purpose of it is to interrupt things. Then you can use
pthread_kill para interrumpir el
read (or any other syscall) in a particular thread. This has the advantage that you don't have to switch your code to using C++/Java-type idioms. You can handle the
EINTR error by checking a flag (indicating whether the thread was requested to abort) and resume the read if the flag is not set, or return an error code that causes the caller to clean up and eventually
If you do use interrupting signal handlers, make sure all your syscalls that can return
EINTR are wrapped in loops that retry (or check the abort flag and optionally retry) on
EINTR. Otherwise things can break badly.