PDA

View Full Version : Getting all Threads




MacDonaldsd
Sep 20, 2008, 07:11 AM
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
Sep 20, 2008, 09:55 AM
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:object: 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
Sep 20, 2008, 10:39 AM
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:object: 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
Sep 20, 2008, 10:53 AM
Well, I still don't understand what it means to "post a notification in all threads".

MacDonaldsd
Sep 20, 2008, 10:58 AM
Well, I still don't understand what it means to "post a notification in all threads".

If a Notification is posted in one thread, I want all objects to receive this notification regardless of what thread they are in.

Sayer
Sep 20, 2008, 11:07 AM
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

MacDonaldsd
Sep 20, 2008, 11:12 AM
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

Thanks Sayer,

I already found that article I just wanted to make sure there was not a "easier" option before I integrated that into my application.

And like the article says, that method has some limitations.

kpua
Sep 20, 2008, 11:21 AM
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:onThread:withObject:waitUntilDone:, which is new in Leopard, instead of dealing with the mach port stuff.