Re: Question on thread communication
Re: Question on thread communication
- Subject: Re: Question on thread communication
- From: Kaelin Colclasure <email@hidden>
- Date: Mon, 20 Mar 2006 11:52:24 -0800
On Mar 20, 2006, at 11:39 AM, Karim Morsy wrote:
Hi,
I'm having an app in which several threads need to communicate with
each other.
I've done this as follows:
on the main thread I detach a new thread that performs a task. upon
completion of the thread the main thread should perform subsequent
steps. it shouldn't do so unless the detached thread has really
finished execution. for the detached thread to signal to the main
thread that it's done, I do this:
NSNotificationCenter *nc= [NSNotificationCenter defaultCenter];
NSNotification *n= [NSNotification notificationWithName:
KMFileInitCompletedNotification object:self];
[nc performSelectorOnMainThread:@selector(postNotification:)
withObject:n waitUntilDone:YES];
[pool release];
the completion steps are then performed by the main thread in the
appropriate handler method.
so the questions I have are:
- is there anything problematic about this approach ?
Aside from the limitation that it can only deliver notifications to
the main thread, no. It's a widely used idiom for updating Cocoa GUIs
in response to background thread activity.
- is there an easier/better way to do this ?
On Tiger and later systems, you can use Darwin notifications. Any
thread can register for them, and they are trivial to post:
notify_post("self.fileInitCompletedNotification");
If you elide the "self." prefix, they can even cross process boundaries.
HTH,
-- Kaelin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden