View Full Version : NSMachPort Vs NSMessagePort

Apr 26, 2011, 12:50 AM

What is the difference between NSMachPort and NSMessagePort ? And When should I use which ?


Apr 26, 2011, 02:21 AM
NSMachPort implementes NSPort using a Mach port. It's very efficient for IPC on the same machine. If you want to do IPC across different machines, you must use NSSocketPort.

NSMessagePort is also limited to same machine IPC like NSMachPort. I don't know what communications method it uses, I've never used or explored it.

I quote from the NSMessagePort class reference.

Important: Avoid NSMessagePort. There's little reason to use NSMessagePort rather than NSMachPort or NSSocketPort. There's no particular performance or functionality advantage. It is recommended avoiding its use.

NSMessagePort may be deprecated in the Mac OS X v 10.6 or later.

EDIT: I did some more reading around this. It seems NSMessagePort was added when NextSTEP evolved into OPENSTEP because Sun couldn't implement NSMachPort on Solaris. So it seems NSMessagePort was meant to be a cross-platform alternative to NSMachPort.

GNUStep doesn't provide NSMachPort. So if you want your code to be compilable under both Cocoa and GNUSTEP, you'd need to ignore Apple's advise and use NSMessagePort.

But really, you should only use NSPort* variables. That way your code will with be portable both across Cocoa and GNUSTEP, and across local and remote ports (assuming you have properly designed distributed objects, eg no fine-grained messaging).

Apr 26, 2011, 06:33 AM
That simplified my search !! Thanks much.

Can make NSMachPort Server Program as a Daemon thread? so that multiple applications can talk to that service?

Apr 26, 2011, 08:23 AM
Yes, they could be used directly for this purpose, coupled with a NSRunLoop in the daemon.

But normally they're used in conjunction with NSConnection as part of Cocoa's distributed objects. See the Distributed Objects Programming Topics guide.