Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does suspending a dispatch queue suspend its target queue?

I want to create two serial queues A & B. Where queue B is a target of queue A. I want to queue up some blocks on B, and suspend it until i'm ready to execute them, however i want to continue executing blocks on queue A.

If i suspend B, will this also suspend it's target queue (queue A)?

My thinking is that i want to schedule these particular blocks (on Queue B) for execution at a later (unspecified) date however i don't want them to ever run concurrently (This involves Core Data ^_^) and i don't want to deal with semaphores. But i want Queue A to continue processing it's blocks, while B is suspended

In case that wasn't clear here's some sample code

dispatch_queue_t queueA = dispatch_queue_create("app.queue.A");
dispatch_queue_t queueB = dispatch_queue_create("app.queue.B");

dispatch_set_target_queue( queueB, queueA );

dispatch_suspend( queueB );
/*
*   Add a bunch of blocks to queue B or A 
*   Where the ones added to A should execute immediately
*/


//Wait till blocks on queue A have finished and start up B
dispatch_resume( queueB );

dispatch_release(queueA);
dispatch_release(queueB);
like image 888
Jonathan Avatar asked Jul 06 '11 21:07

Jonathan


People also ask

How does queue dispatch work?

A dispatch queue that is bound to the app's main thread and executes tasks serially on that thread. A dispatch queue that executes tasks concurrently using threads from the global thread pool. A dispatch queue that executes tasks serially in first-in, first-out (FIFO) order.

How many types of dispatch queues are there?

There are two types of dispatch queues, serial dispatch queues and concurrent dispatch queues.

What is dispatch queue QoS?

A quality-of-service (QoS) class categorizes work to perform on a DispatchQueue . By specifying the quality of a task, you indicate its importance to your app. When scheduling tasks, the system prioritizes those that have higher service classes.

Are dispatch queues thread safe?

Yes, the dispatch queue objects, themselves, are thread-safe (i.e. you can safely dispatch to a queue from whatever thread you want), but that doesn't mean that your own code is necessarily thread-safe.


1 Answers

dispatch_set_target_queue(queueB, queueA);
dispatch_suspend(queueB);

queueB is suspended, but queueA is not suspended.

dispatch_set_target_queue(queueB, queueA);
dispatch_suspend(queueA);

queueA and queueB are suspended.

like image 117
Kazuki Sakamoto Avatar answered Sep 22 '22 20:09

Kazuki Sakamoto