Re: How to know, which NSThread has terminated?
Re: How to know, which NSThread has terminated?
- Subject: Re: How to know, which NSThread has terminated?
- From: Shaun Wexler <email@hidden>
- Date: Sat, 29 May 2004 01:26:52 -0700
On May 29, 2004, at 12:18 AM, Philip Mvtteli wrote:
>
>> I have no idea how to recognize this just created thread later, when
>
>> it terminates. What I need is something like a handle. I mean
>
>> objc_thread_detach() gives back an objc_thread_t too.
>
>> I could put some flag into the tread dictionary, but it would be so
>
>> much nicer to be able to just ask the thread for its handle or to
>
>> keep the address of the NSThread instance.
>
>
>
> Here's an idea: Send a notification to the main thread using
>
> -performSelectorOnMainThread: with the NSThread object (using
>
> NSThread's +currentThread class method to obtain an address) as the
>
> notification's embedded object. I don't see why that wouldn't work,
>
>
Good idea, but I create a lot of threads from different objects. How
>
can I be sure, that not in the same moment (or slightly before or
>
afterwards) another thread is forking a thread, so sending this
>
notification? Or, as far as I know, performInMainThread would be
>
executed, when the main thread enters the next time the runloop. So I
>
could have forked several other threads, which' notifications would
>
then be subsequently queued in the event queue.
Written in Mail.app:
extern NSMutableDictionary *managedThreads; // assume this exists
- (void)detachManagedThreadSelector:(SEL)selector toTarget:(id)target
withObject:(id)identifier
{
NSMethodSignature *signature = [target
methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation
invocationWithMethodSignature:signature];
[invocation setTarget:target];
[invocation setSelector:selector];
[invocation setArgument:identifier atIndex:2];
[invocation retainArguments];
[NSThread detachNewThreadSelector:@selector(managedThreadSelector:)
toTarget:self withObject: invocation];
}
- (void)managedThread:(id)invocation
{
NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:NULL]
init];
id identifier;
[invocation getArgument:&identifier atIndex:2];
if (identifier) {
@synchronized(self) {
[managedThreads setObject:[NSThread currentThread]
forKey:identifier];
}
}
@try {
// set up any connections, ports, pipes, etc.
[invocation invoke];
}
@catch (NSException *exception) {
[NSApp reportException:exception];
}
@finally {
// clean up connections, etc.
}
if (identifier) {
@synchronized(self) {
[managedThreads removeObjectForKey:identifier];
}
}
[pool release];
}
Using the NSInvocation in this way allows you to pass several
additional arguments to a new thread, if necessary (not shown).
--
Shaun Wexler
MacFOH
http://www.macfoh.com
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.