On Jun 16, 2008, at 11:17 AM, j o a r wrote:


On Jun 16, 2008, at 7:44 AM, David wrote:

While I've been investigating this, I have found more documentation on thread safety issues in the "Threading Programming Guide". I'd read it before but it helps to re-read. It says that NSView classes ARE thread
safe... to an extent.


Very interesting that it says that NSView is "generally thread- safe", but then immediately afterwards limits you to "perform other operations [...] only from the main thread". Note the "other", which is as broad in scope as the "generally" used earlier. I'm going to file an enhancement request for improved documentation here.

I think that what they want to call out there is that you can draw from background threads. I could be wrong, but I don't think that you should read more into it than that.

This level of vagueness is persistent throughout the framework docs, though, and gives them an absolute value |v| < 0 with respect to threading (my opinion, of course). And yes, I know the "file a bug report" mantra, but a) I don't get paid for filing bug reports, and b) it can take years even for a doc bug to be fixed (my oldest is from 2005, but I expect it's just forgotten).

Quotes from the docs:
"For the most part, the Application Kit is not thread safe; in particular, Cocoa bindings and controllers are not thread safe—if you are using these technologies, multi-threading may be complex." [1]

Now juxtapose that with this statement:

"For example, the Application Kit is generally thread-safe, but its text system (consisting of NSText, NSTextView,NSLayoutManager, NSTextStorage, and related classes) is not thread-safe if the layout of text is done asynchronously." [2]

So can you draw text to a bitmap context in a background thread? Sure; use NSStringDrawing, or use NSLayoutManager directly with background layout disabled. Only the latter option is (sort of) documented and /only in the header/.

If you want to (say) open a URL from a secondary thread, can you use - [NSWorkspace openURL:]? Apparently not, but it's not in either list [3]. Until recently, NSURL was on the thread-unsafe list, so maybe that was the answer...

However, can you use LaunchServices? Certainly! It says so, right in the API docs for LSOpenCFURLRef: "Thread-safe since Mac OS version 10.2." [4]. Perfect. So if you want thread safety, use Carbon since it tends to have explicit documentation ;).

This ambiguity also bites Apple, I think. For instance, prior to 10.5, SearchKit (which is documented to be thread safe) would crash using NSAttributedString to read certain files (XML/HTML, IIRC). NSImage caching has also been mentioned on this list as the source of crashes, yet it's documented to be thread safe.

regards,
Adam

[1]
http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdMultiThreading.html

[2] 
http://developer.apple.com/documentation/UserExperience/Conceptual/Quicklook_Programming_Guide/QLImplementationOverview/chapter_5_section_4.html

[3]
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/chapter_950_section_2.html#/ /apple_ref/doc/uid/10000057i-CH12-SW4

[4]
http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to