Re: Thread Safe APIs?
Re: Thread Safe APIs?
- Subject: Re: Thread Safe APIs?
- From: "Dennis C. De Mars" <email@hidden>
- Date: Wed, 27 Jun 2001 07:28:11 -0700
on 6/26/01 3:43 PM, Steve Gehrman at email@hidden wrote:
>
Is there a list of thread safe Cocoa APIs? I'm trying to write a
>
multithreaded app and am getting mysterious random crashes. I wish
>
there was an easier way to figure out what I'm calling that I shouldn't
>
be calling.
If you haven't already, you'll want to read the following:
http://developer.apple.com/techpubs/macosx/ReleaseNotes/ThreadSupport.html
Also, as the poster quoted below mentioned, the TrivialThreads and
SimpleThreads samples are highly recommended.
on 6/26/01 8:13 PM, Esteban at email@hidden wrote:
>
I'm not sure if there's a official and full list of thread safe Cocoa
>
APIs, but I remember reading something about it not being a good idea to
>
call AppKit APIs directly in a multithreaded app. For example, you
>
can't modify an NSTextField directly from a secondary thread in your
>
app. Instead you should have a function in your primary thread that
>
handles and distributes the request.
>
>
The idea is that a secondary thread puts in a request to call the method
>
that will update the NSTextField to the NSRunLoop in the main thread.
>
When the main thread is done with whatever it was doing it will see the
>
request and call things to be updated appropriately.
>
>
At least this is what I understood from the TrivialThreads and
>
SimpleThreads examples at Apple's site. It is also how I'm handling
>
updating of any elements in my own multithreaded application.
>
>
Also I think that all of the Foundation APIs are thread-safe.
The link I quoted above may not be up to date, but it lists several
Foundation classes that are not thread-save (including all of the mutable
collection classes).
A quote from the notes with TrivialThreads:
"... very few classes in the Cocoa framework are re-entrant. [The lower
layers of Foundation are re-entrant, but none of the AppKit is.] So you can
do your networking in threads, but you can't modify the user interface from
your threads"
Also, NSLock is listed as being thread-safe, but the description says NSLock
instances must be created before going multi-threaded, or, if created after
going multi-threaded, that code must itself be protected by a lock. This
sound to me like NSLock is not thread-safe...an odd situation. At any rate,
it is something you need to be aware of.
- Dennis D.