I have an objective-c class with some methods that use the GCD queue to provide concurrent access to the resource in series (the standard way to do this).
Some of these methods require calling other methods of the same class. Therefore, the locking mechanism must be re-enabled. Is there a standard way to do this?
I used each of these methods first
dispatch_sync(my_queue, ^{
to synchronize access. As you know, when one of these methods calls another such method, a deadlock occurs because the dispatch_sync call stops the current executable file until another block is executed, which also cannot be executed, because execution in the queue has stopped. To solve this problem, I then used, for example, this method:
- (void) executeOnQueueSync:(dispatch_queue_t)queue : (void (^)(void))theBlock { if (dispatch_get_current_queue() == queue) { theBlock(); } else { dispatch_sync(queue, theBlock); } }
In each of my methods I use
[self executeOnQueueSync:my_queue : ^{ // Critical section }]
I do not like this solution, because for each block with a different return type, I need to write a different method. Moreover, this problem looks very often to me, and I think that for this there should be a more acceptable, standard solution.
objective-c locking grand-central-dispatch reentrantlock
Daniel S. Oct. 21 '13 at 12:12 2013-10-21 12:12
source share