Edit AllPages

Pages in this topic: [Topic]

When writing a General/MultiThreaded application, there are several issues that you need to be aware of:

In order to preserve General/ThreadSafety, you can send everything you use a retain message, you can employ an General/AutoReleasePool and General/AutoRelease objects, or you can use the concept of General/WeakReferences to avoid General/RetainCycles.

**see also: ** General/CanTwoThreadsLockFocusOnTheSameView

I know for a fact that updating an General/NSTextView from a worker thread is unsafe. I know for a fact that working with icons (via General/NSWorkspace) in a worker is unsafe. I strongly suspect all menu operations are unsafe. Pending more complete documentation, I’d assume everything in General/AppKit is unsafe unless told otherwise (such as drawing and other topics touched on in the release note).

Find some trivial examples in:


You can use its code snippets to implement a simple client/server that uses mach messages to pass messages. To aid development, build the server into an Application (so General/AppKit manages the run loop for you) and create a command-line tool client or an Application client, whichever is easier. That should give you a leg-up on writing us some sample code ;-).

When making your app thread-safe, General/NSConnection.html again suggests using mach-ports, and illustrates exactly how to do it. I was able to just plug it into an existing project of mine.

There are some less-elementary examples of using DO on TCP/IP on the system here:


See also


Thomas Lachand-Robert also wrote a whole page about communication between threads, and in particular to General/AppKit: Don’t hesitate to send him any suggestions for additions or changes.

I also added some General/DistributedObjectsSampleCode from the General/MacOsxDev list.

– General/StevenFrank

This recent post from General/MacOsxDev may also be of interest:

From: Toby Paterson Subject: Inter-thread messaging: a solution

The topic of how to communicate between two threads efficiently has come up a number of times while I’ve been on this list. I’ve written some code to facilitate this and am making it available for your use and abuse.

The model expects that you have one or more worker threads that need to post information back to the main, UI thread, though it can be used to communicate with any thread running a run loop. It takes the form of categories on General/NSObject and General/NSNotificationCenter to allow you to invoke methods and post notifications in a specific thread:

 [master performSelector:@selector(workerDone:)

 General/[[NSNotificationCenter defaultCenter]
     beforeDate:General/[NSDate distantFuture]];

The implementation uses General/NSPortMessages to marshall the message to the target thread. It’s more light-weight than DO - General/NSPortMessage and General/NSPort are a fairly thin veneer on top of mach messaging - but not nearly as general. Make sure you read the comments in the header for some caveats of the argument marshalling.

You can download it from:

I’m releasing this code in to the public domain: use it; modify it; ship it in free or commercial software; just don’t expect any support nor assign to me any blame if using this code causes you any mental or physical harm. Have fun.

– General/StevenFrank

The above page is no longer up, but i have mirrored the code. See General/InterThreadMessaging. – General/DustinVoss

I have made a utility class, General/FDObjectiveMethod, that allows you to treat a method as an object. You initialize it with a complete (and that part is important, because if it’s incomplete, you’ll get errors) General/NSInvocation, and then start it with the -execute method. You can halt it part way through with the -stopExecution method.

It runs the invocation in a separate thread, so you don’t have to worry about that.

It’s here on General/CocoaDev, on the General/ObjectiveMethod page.

– General/RobRix

(It’s also a better idea to use General/ThreadWorker. General/FDObjectiveMethod has fallen by the wayside. – General/RobRix)

Just starting some rewriting. – General/KritTer