Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Hi,

I have a multithreaded application and I would like to post a NSNotification on all threads in an application. Is there an OS call for getting all the threads for a given application ?
 

kpua

macrumors 6502
Jul 25, 2006
294
0
What exactly are you trying to accomplish. What you said doesn't really make sense, given how NSNotificationCenter works:

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

So, in other words, NSNotificationCenter doesn't really have any concept of threads. Any object registered as an observer for a notification will receive that notification when it's posted. You may be thinking that your observer objects are "on" a particular thread, but they're not. They're on the heap, which makes them app-global.

However, you may have different objects which are used only for particular threads, and I think you want to execute certain code on each of those threads when a notification is posted. I suggest you just have a single observer for your notification and use regular inter-thread communication to invoke the code you want on each thread.

Unfortunately, there's no way get all threads in an application with NSThread. (They may not all be NSThreads, perhaps just plain pthreads.) Instead, you can maybe keep track of them as they're created with initWithTarget:selector:eek:bject: or in the objects you've dedicated to different threads. (When your thread starts store the result to [NSThread currentThread] so your other thread can access it.

Does that help?
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
What exactly are you trying to accomplish. What you said doesn't really make sense, given how NSNotificationCenter works:



So, in other words, NSNotificationCenter doesn't really have any concept of threads. Any object registered as an observer for a notification will receive that notification when it's posted. You may be thinking that your observer objects are "on" a particular thread, but they're not. They're on the heap, which makes them app-global.

However, you may have different objects which are used only for particular threads, and I think you want to execute certain code on each of those threads when a notification is posted. I suggest you just have a single observer for your notification and use regular inter-thread communication to invoke the code you want on each thread.

Unfortunately, there's no way get all threads in an application with NSThread. (They may not all be NSThreads, perhaps just plain pthreads.) Instead, you can maybe keep track of them as they're created with initWithTarget:selector:eek:bject: or in the objects you've dedicated to different threads. (When your thread starts store the result to [NSThread currentThread] so your other thread can access it.

Does that help?

Hi,

This issue is I know how NSNotificationCenter works in regards to threads, but that isn't what I want. I want a notification to be posted to all threads, and I am just trying to figure out the best way to do it.

The logic in my code at the moment is that the main thread spawns a thread which in turn spawns another thread. So as the main thread doesn't have access directly to both threads, I was just trying to make sure I didn't miss and easy way of keeping track of the threads.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Well, I still don't understand what it means to "post a notification in all threads".
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK

kpua

macrumors 6502
Jul 25, 2006
294
0
There is this invention called "google" that can reveal information with a simply plain text query like "cocoa notify all threads" which returns this:

http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html

That is essentially what I described above.

I haven't tried it, but it may be easier to do the inter-thread messaging with -performSelector:eek:nThread:withObject:waitUntilDone:, which is new in Leopard, instead of dealing with the mach port stuff.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.