> 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 (Cocoa-dev@lists.apple.com)

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

This email sent to arch...@mail-archive.com

Reply via email to