Re: Block until block is called
Re: Block until block is called
- Subject: Re: Block until block is called
- From: Andreas Grosam <email@hidden>
- Date: Sat, 27 Aug 2011 09:04:13 +0200
On Aug 27, 2011, at 7:07 AM, Glenn L. Austin wrote:
> On Aug 26, 2011, at 9:40 PM, Brian Norh wrote:
>
>> Hi.
>>
>> I'm working against an API which looks like this:
>>
>> - (void)performOperationWithBlock:(void (^)(void))block
>>
>> The call returns directly and the execution continues. At some point
>> the block is eventually called. How would I do to block execution
>> until the block have been called?
See, dispatch_sync and dispatch_barrier_sync. You may use these functions in your implementation of your method.
>>
>> [obj performOperationWithBlock:^(void) {
>> // Do something here
>> }];
>> // Block here until the block above have been called.
>
> Um, why would you want to (or need to) do that?
This can be used to synchronize access to shared resources, and wait for the effect on the resource.
Suppose an object can be accessed from different threads. The access is serialized through a dispatch queue, dedicated to access this object. Then, you want to send obj a message, and be sure that this message has been received by the object in the next statement:
id msg = …;
dispatch_sync(obj_queue, ^{
[obj message:msg];
});
// at this point, you know obj received "message" with parameter "msg".
The code above is equivalent to:
pthread_mutex_lock(obj_mutex);
[obj message:msg];
pthread_mutex_unlock(obj_mutex);
Andreas
_______________________________________________
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