Re: -[NSThread start] blocks ?!?
Re: -[NSThread start] blocks ?!?
- Subject: Re: -[NSThread start] blocks ?!?
- From: John Daniel <email@hidden>
- Date: Mon, 28 Sep 2015 08:44:19 -0400
> On Sep 28, 2015, at 12:13 AM, email@hidden <mailto:email@hidden> wrote:
>
> Thank you, John. You are referring to what my secondary thread is doing. My point is that if we can’t at least rely on -[NSThread start] returning before the secondary starts, there is no way for a seconary thread to call back to the main thread without possibility of deadlock. It’s a defect in NSThread.
>
>> Data sharing using serial queues is safer and more robust by design.
>
> OK, if it is a defect in NSThread, GCD might not have this defect. I’ll consider that. Thank you.
It is not a defect in NSThread. That is just a subtlety the way it works. In this model, there are three potential situations when -[NSThread start] returns:
1) Your thread hasn’t started yet
2) Your thread has started and is running
3) Your thread has completed
Your code must handle all of these possibilities.
GCD is just a different model and has different subtleties. GCD may not spawn any new “threads” at all. You would think in terms of queues and blocks instead.
GCD encourages a more straightforward producer-consumer architecture. You can use that architecture for your NSThread code too and get more robust behaviour. In general, you really don’t want to think about what your threads might or might not be doing or what state they might be in. Use locks around any structures that could be shared. Use semaphores to enforce any sequential behaviour if you need that.
John Daniel
email@hidden <mailto:email@hidden>
John Daniel
email@hidden
_______________________________________________
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