Re: Terminating an NSThread from another thread
Re: Terminating an NSThread from another thread
- Subject: Re: Terminating an NSThread from another thread
- From: Kyle Moffett <email@hidden>
- Date: Mon, 10 Feb 2003 17:18:17 -0500
On Sunday, Feb 9, 2003, at 21:54 US/Eastern, Peter Fischer wrote:
Is it possible to kill an NSThread that is blocking from another
thread? Looking at the documentation, it does not appear that you are
able to send the kill command to a thread other than your current
thread.
Is there ways to kill a blocking thread from another thread? Any
help would be greatly appreciated...
mydata_t some_func_to_timeout(void);
Instance variable datavar of type mydata_t
- (void)forkThread {
NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:0];
[NSThread detachNewThreadSelector:@selector(forkedThread:)
toTarget:self withObject: lock];
sleep 10; // Timeout here
[lock lock];
if ([lock tryLockWhenCondition:1]) {
// Handle the data here (In datavar)
} else {
// Handle timeout here
}
[lock release]; // The detachNewThread stuff retains the object during
the execution of the thread
// Therefore, we don't need to worry about retaining it anymore
}
- (void)forkedThread:(NSConditionLock *)lock {
mydata_t temp;
temp = some_func_to_timeout();
if ([lock tryLockWhenCondition:0]) {
datavar = temp;
[lock unlockWithCondition:1];
}
}
This was written in Mail, so it probably won't work as written here,
but you get the idea.
This kind of thing allows your code to exit cleanly, so you don't get
crazy unfreed
memory stuff. It's always best to allow the threading system time to
cleanup its mess.
HTH,
Kyle Moffett
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCM/CS/IT/U d- s++:- a16 C++++>$ UB/L/X/*++++(+)>$ P+++(++++)>$
L+++(++) E W++(+) N+++(++) o? K? w---(-) O? M++ V? PS+() PE+(-) Y+
PGP? t+(+++) 5 X R? tv-(--) b++++(++) DI+ D+ G e->++++$ h! !r-- !y?
------END GEEK CODE BLOCK------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.