Re: Providing synch/asynch API
Re: Providing synch/asynch API
- Subject: Re: Providing synch/asynch API
- From: Jean-Daniel Dupas <email@hidden>
- Date: Tue, 25 Mar 2008 17:30:34 +0100
In case you choose option 2.
For the sync version, as you want to block the execution thread, you
don't have to run the runloop. You may use a condition lock to wait
the worker thread completion (NSConditionLock or pthread condition).
performSync() {
// create condition
performAsync().
// wait condition
return result;
}
and in worker thread
performAsync() {
// do some works.
if (condition) // set condition (that will unlock the caller thread)
}
Le 25 mars 08 à 17:05, Andy Klepack a écrit :
I have several methods for which I'd like to provide asynchronous
and synchronous variants. These methods are not simple operations
and have non-void return values. Is there a good model to follow in
implementing this? I don't recall ever seeing a technique for this
in any of the Cocoa documentation I've looked at.
In my mind I've got two options:
1. The synchronous version (fooSynch) is either the implementation
or calls private implementation (_foo) on the same thread. The
asynchronous version would detach a new thread to some method
(threadedFood) that runs fooSynch and then takes care of calling
callbacks, informing a delegate, etc.
2. The asynchronous version detaches a new thread to the
implementation (_foo) that contains the operation and any necessary
calls to callbacks, delegates, etc. The synchronous version wraps a
call to the asynchronous version and somehow blocks the thread from
returning until the asynchronous call completes. I'm not sure how to
do this. I imagine it may involve installing ports in the run loop
(over which the thread could communicate) and then running the run
loop until the thread signals completion. That sounds a little too
contrived.
I'm curious how people tackle this problem pre-Leopard and
performSelector:onThread:withObject:waitUntilDone: API
Thanks,
-Andy
_______________________________________________
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
_______________________________________________
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