Re: Exiting NSThread
Re: Exiting NSThread
- Subject: Re: Exiting NSThread
- From: Ondra Cada <email@hidden>
- Date: Sun, 30 Sep 2001 10:31:03 +0100
Dustin,
>
>>>>> Dustin Mierau (DM) wrote at Sat, 29 Sep 2001 14:52:05 -0700:
DM> What I meant was I have a thread which connects using bsd
DM> sockets to say an ftp server, now i am in the middle of
DM> downloading a file in a while loop, now the app quits, how do i
DM> exit that thread cleanly? I want to obviously close the
DM> connection to the server and exit the thread.
Connection will be closed automatically for you (just like almost all other
task resources), and server will get an appropriate notification
(NSConnectionDidDieNotification in case of DO, or its socket-based equivalent
at the BSD level, which I don't remember, but which does exist).
If you *do* want to do it "explicitly", you have to provide a means for the
thread to communicate, probably a global (or DO, but in this case, global's
much simpler):
static volatile BOOL _ceaseAllActivities=NO,_done=NO;
// secondary thread code
NS_DURING
...
...
... // innermost loop
if (_ceaseAllActivities) [NSException raise:XXX format...];
...
...
...
NS_HANDLER
NS_ENDHANDLER
... cleanup ...
_done=YES;
// main thread code
[NSThread detach....];
...
if (want_to_quit) {
_ceaseAllActivities=YES;
while (some_timeout && !_done)
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
Or something alike.
Though, so far as I know, there is no real reason to do that. You have to
consider that the application might crash anyway -- in which case the
automatic cleanup will be the same as if you just exit.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc