Re: slightly OT (unblocking accept)
Re: slightly OT (unblocking accept)
- Subject: Re: slightly OT (unblocking accept)
- From: Mike Davis <email@hidden>
- Date: Sat, 2 Feb 2002 21:51:42 -0500
On Saturday, February 2, 2002, at 09:31 PM, cocoa-dev-
email@hidden wrote:
Date: Sat, 2 Feb 2002 15:48:36 -0800
Subject: slightly OT (unblocking accept)
From: email@hidden
To: Cocoa Dev Mailing List <email@hidden>
I'm using SmallSockets and have a thread blocked in accept() waiting on
external connections.
I'm wondering if there's a way to stop that thread. All I can see from
the NSThread documentation is the class method + exit; which seems like
it must be called from the thread that wants to exit, not an external
one.
Okay, you need the concept of "cancelable threads". Basically, the
thread should be cancelable on any blocking call (accept(), read(),
write() or mutex/semaphores).
I've done it having an abstract blocking call class (a protocol would be
okay) which can register itself with the current thread. When another
thread wants to kill the thread off, it "cancels" the blocking call,
having set a "cancelling" flag.
Okay, in the case of a blocking socket call, the cancel method of the
blocking call implementation just closes the socket with close(). When
the blocking call fails, which is what will happen if the socket is
closed by another thread, it checks the "cancelling" flag to see if the
thread is supposed to die. If so, it throws an exception which can be
used for cleanup etc.
The exception propagates and eventually you'll come back to the thread
method and can gracefully exit.
Is there some magic fcntl() or other NSThread magic that I can use to
tell the thread blocked in accept() to unblock so i can kill it?
thanks.
-tw