Re: -[NSThread start] blocks ?!?
Re: -[NSThread start] blocks ?!?
- Subject: Re: -[NSThread start] blocks ?!?
- From: John Daniel <email@hidden>
- Date: Sun, 27 Sep 2015 20:01:05 -0400
> In a OS X app, predating Grand Central Dispatch, in the main thread, I create and start a new thread
>
> NSThread* worker ;
> worker = [[NSThread alloc] initWithTarget:instance
> selector:@selector(beginWithInfo:)
> object:info] ;
> [worker setName:@“My Worker thread"] ;
> [worker start] ;
>
> usually this works OK, but sometimes, possibly when the above code executes a second or third time in rapid succession, [worker start] will block forever:
There is no way to tell what is causing the deadlock without knowing exactly what is happening relating to:
1) instance
2) beginWithInfo:
3) info
If this code pre-dates GCD, you could have all kinds of nasty stuff going on. All of the items I listed above are shared. They may, or may not, need to be protected by locks. There is no way to tell without dumping virtually your entire program.
I strongly suggest looking into adopting GCD instead. It dates to 10.5, so you aren’t giving up too much backwards compatibility. Data sharing using serial queues is safer and more robust by design. You will still need to ensure that any shared access uses that serial queue, but I find GCD much more straightforward. You may end up blocked in a semaphore again, but it will be your own semaphore and you can more easily debug it.
John Daniel
_______________________________________________
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