• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Providing synch/asynch API
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Providing synch/asynch API (From: Andy Klepack <email@hidden>)

  • Prev by Date: Re: Alternate row colors and selection
  • Next by Date: Re: How to delay application quit?
  • Previous by thread: Providing synch/asynch API
  • Next by thread: RE: Providing synch/asynch API
  • Index(es):
    • Date
    • Thread