ios - How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()? -


recently, had need function use guarantee synchronous execution of given block on particular serial dispatch queue. there possibility shared function called running on queue, needed check case in order prevent deadlock synchronous dispatch same queue.

i used code following this:

void runsynchronouslyonvideoprocessingqueue(void (^block)(void)) {     dispatch_queue_t videoprocessingqueue = [gpuimageopenglescontext sharedopenglesqueue];      if (dispatch_get_current_queue() == videoprocessingqueue)     {         block();     }     else     {         dispatch_sync(videoprocessingqueue, block);     } } 

this function relies on use of dispatch_get_current_queue() determine identity of queue function running on , compares against target queue. if there's match, knows run block inline without dispatch queue, because function running on it.

i've heard conflicting things whether or not proper use dispatch_get_current_queue() comparisons this, , see wording in headers:

recommended debugging , logging purposes only:

the code must not make assumptions queue returned, unless 1 of global queues or queue code has created. code must not assume synchronous execution onto queue safe deadlock if queue not 1 returned dispatch_get_current_queue().

additionally, in ios 6.0 (but not yet mountain lion), gcd headers mark function being deprecated.

it sounds should not using function in manner, i'm not sure should use in place. function above targeted main queue, use [nsthread ismainthread], how can check if i'm running on 1 of custom serial queues can prevent deadlock?

assign whatever identifier want using dispatch_queue_set_specific(). can check identifier using dispatch_get_specific().

remember dispatch_get_specific() nice because it'll start @ current queue, , walk target queues if key isn't set on current one. doesn't matter, can useful in cases.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -