Waiting for callback with GCD: impossible?
Waiting for callback with GCD: impossible?
- Subject: Waiting for callback with GCD: impossible?
- From: Jens Alfke <email@hidden>
- Date: Wed, 08 Oct 2014 11:59:23 -0700
On a related topic from my last post, looking at the equivalent situation but using GCD instead of runloops, the problem appears to be impossible. That is:
* Code running on dispatch queue A starts some async task that will run on another queue B, and which will signal completion by using dispatch_async to call a completion block on queue A, passing it some result.
* After the task starts, the logic on queue A decides it needs to block until the task finishes and returns its result. (This might be part of an implementation of the 'future' design pattern, for example.)
* Now, how does queue A wait for the callback? It seems to be impossible: the only way for code running on queue A to receive the callback is to return back to the queue dispatcher, so if it tries to wait it simply blocks the queue, causing a deadlock.
This is exactly why runloops have the ability to run a "nested" loop, to block but still allow incoming messages. Of course that comes with its own set of complications, like the necessity for multiple "modes", and issues like the one I just brought up.
But given that dispatch queues can't do this, what's the solution to this design problem, i.e. turning an async call into a sync one?
—Jens
_______________________________________________
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