Re: Multithread communication
Re: Multithread communication
- Subject: Re: Multithread communication
- From: Kyle Sluder <email@hidden>
- Date: Wed, 3 Feb 2010 08:54:04 -0800
On Wed, Feb 3, 2010 at 8:30 AM, McLaughlin, Michael P. <email@hidden> wrote:
> In a multithreaded app using NSOperationQueue and targeting Leopard, I am
> having an inordinate amount of trouble communicating the results of the
> thread computations to the main thread which acts as an overall coordinator.
Welcome to multithreaded programming. :)
> Specifically, I am getting obscure and sporadic crashes (about 10 percent of
> the time), some citing bad access and others unknown but which seem to
> suggest that things are being done when the threads can no longer do them
> (or something akin to that). All of these crashes occur deep in the system,
> not in my code.
They are almost certainly your fault, though. What stinks about
multithreading is that a mistake has an excellent chance of
manifesting itself as a problem somewhere completely unrelated to
where that mistake has been made.
> The threads read data upon initialization but do not write to any structures
> but their own. I have to pass these filled structures back to the main
> thread upon completion. The last thing each thread does is
> postNotificationName to the main thread saying that it is ready to return
> lots of data which it does via an NSPointerArray
> [ptr pointerArrayWithStrongObjects]
> and dictionary. The object which collects the results has a function
> getOpResults() which accesses the passed pointer.
It is not useful to discuss threading problems without the actual code
that you're having trouble with.
> I thought that this should all be correct but somehow it crashes, at times
> immediately, at other times only when everything is done and, 90 percent of
> the time, not at all! I have attempted various remedies, including a static
> NSLock for getOpResults(), retaining the NSOperations for a while in the
> main thread, using a dependent signal to say that all threads should be
> finished, using a countdown int to say the same thing. So far, nothing has
> solved the problem.
You're thrashing. Threading must be approached from a mindset of correctness.
> Could someone recommend the "best" (most robust) way to pass back data from
> threads to the main thread? Is there a good way to probe crashes such as
> these? Right now, I cannot even tell where the error is really occurring.
The best kind of shared data is no shared data at all. Your threads
should receive copies of the data they need to operate, and return
copies of the data they have computed. Which thread owns the data is
handed off at those return boundaries.
Beyond that, you'll need to post your code. (That is, the actual code
you are feeding the compiler. Pseudocode, descriptions of code, and
redacted versions of code are all useless here.)
--Kyle Sluder
_______________________________________________
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