[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to know, which NSThread has terminated?

From: Shaun Wexler
Subject: Re: How to know, which NSThread has terminated?
Date: Sat, 29 May 2004 01:38:10 -0700

On May 28, 2004, at 4:57 PM, Philip Mötteli wrote:

When I fork a thread using

+(void)detachNewThreadSelector:(SEL)aSelector toTarget:aTarget withObject:anArgument

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. But because there's void as the return value, all this is not possible.
How do people keep track of a specific thread, using NSThread?

Written in Mail.app:

- (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 retain] atIndex:2];

[NSThread detachNewThreadSelector:@selector(managedThread:) toTarget:self withObject:invocation];

- (void)managedThread:(id)invocation
NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:NULL] init];

        id identifier;
        [invocation getArgument:&identifier atIndex:2];

        if (identifier) {
                @synchronized(managedThreads) {
[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(managedThreads) {
                        [managedThreads removeObjectForKey:identifier];
                        [identifier release];
        [pool release];
Shaun Wexler

Attachment: smime.p7s
Description: S/MIME cryptographic signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]