Re: Multithreading and Mach ports
Re: Multithreading and Mach ports
- Subject: Re: Multithreading and Mach ports
- From: Nathan Day <email@hidden>
- Date: Wed, 10 Dec 2008 18:07:13 +1100
On 10/12/2008, at 07:36 , John Love wrote:
Reference:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/chapter_6_section_5.html#/
/apple_ref/doc/uid/10000057i-CH16-SW7
I've got several questions since I'm just starting to investigate
Mach Ports.
1) Why is +LaunchThreadWithPort: so designated, ie, a class method.
It seems to me that as far as the specific code listings presented,
a class method is not required? For example, [workerObj
sendCheckinMessage:distantPort] could be replaced with [self
sendCheckinMessage:distantPort] if LaunchThreadWithPort would be
changed to an instance method.
2) Maybe the real question #1 is: What exactly is the object in the
background thread, named workerObj ??
Class object can be used as any other object, it means they don't have
to create an instance.
It is the Class/Factory object MyWorkerClass you can treat them the
same as any other object they are just created automatically for you.
When you send a message you do not disquish between class objects and
instance objects it is only when defining the class you use the +/-
syntax to say which object(s) the method is for, the single class
object or the multiple instance objects.
I don't know if this helps but if you where to implement this kind of
thing in C, then each class would have two struct definitions one for
the class object and one for the instances that would contain a
pointer to the class object.
3) I've saved my most general question to last, at least for this
trip: currently when I enter my method to start up my background
thread or threads, I simply call (several times if I have multiple
threads:
[NSThread detachNewThreadSelector:@selector(bgCalcThread:)
toTarget:self withObject:nil]
If I have multiple background threads, I can use a
NSMutableDictionary with multiple keys for inter-thread communication
As long as you use a lock yes, the point of this example is NSRunLoop
and using NSPort, doing it this way mean you can have a thread wait
for to be triggered on mutliple inputs source one of which is data
through your port you could not do this with just passing data
directly between two threads and using locks.
One use for this prior to 10.4? is that you could use this to invoke
methods in the main thread, my NDRunLoopMessager class did this and
there was another on the net somewhere, there is now an NSObject
method to do this for you, it probable uses this technique. You could
not do this with a straight locks very well because you either have to
use I timer so the runloop can keep polling until there is data for it
to handle or it waits on a lock until the data is ready, holding up
the GUI thread.
So, what do Mach Ports give me that the above doesn't?
John Love
Touch the Future! Teach!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden