Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First things first: <code>dispatch_get_current_queue()</code> is deprecated. The canonical approach would now be to use <code>dispatch_queue_set_specific</code>. One such example might look like:</p> <pre><code>typedef dispatch_queue_t dispatch_recursive_queue_t; static const void * const RecursiveKey = (const void*)&amp;RecursiveKey; dispatch_recursive_queue_t dispatch_queue_create_recursive_serial(const char * name) { dispatch_queue_t queue = dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL); dispatch_queue_set_specific(queue, RecursiveKey, (__bridge void *)(queue), NULL); return queue; } void dispatch_sync_recursive(dispatch_recursive_queue_t queue, dispatch_block_t block) { if (dispatch_get_specific(RecursiveKey) == (__bridge void *)(queue)) block(); else dispatch_sync(queue, block); } </code></pre> <p>This pattern is quite usable, but it's arguably not bulletproof, because you could create nested recursive queues with <code>dispatch_set_target_queue</code>, and trying to enqueue work on the outer queue from inside the inner one would deadlock, even though you are already "inside the lock" (in derision quotes because it only <em>looks</em> like a lock, it's actually something different: a queue — hence the question, right?) for the outer one. (You could get around that by wrapping calls to <code>dispatch_set_target_queue</code> and maintaining your own out-of-band targeting graph, etc., but that's left as an exercise for the reader.)</p> <p>You go on to say:</p> <blockquote> <p>I do not like this solution, because for every block with a different return types, I need to write another method.</p> </blockquote> <p>The general idea of this "state-protecting serial queue" pattern is that you're protecting private state; why would you "bring your own queue" to this? If it's about multiple objects sharing the state protection, then give them an inherent way to find the queue (i.e., either push it in at init time, or put it somewhere that's mutually accessible to all interested parties). It's not clear how "bringing your own queue" would be useful here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload