NSPratik
NSPratik

Reputation: 4846

Why do two concurrent queues created by DISPATCH_QUEUE_CONCURRENT not running concurrently?

To execute two tasks concurrently, I made two concurrent queues and dispatched some blocks to both the queues.

Here is what I am doing in main thread

-(IBAction)btn_Pressed:(id)sender
{
    dispatch_queue_t queue_a = dispatch_queue_create("com.gcd_demoA", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue_b = dispatch_queue_create("com.gcd_demoB", DISPATCH_QUEUE_CONCURRENT);

    dispatch_sync(queue_a, ^{
        NSLog(@"A - 1");
    });

     dispatch_sync(queue_a, ^{
        NSLog(@"A - 2");
    });

    dispatch_sync(queue_a, ^{
        NSLog(@"A - 3");
    });

    dispatch_sync(queue_b, ^{
        NSLog(@"B - 1");
    });

    dispatch_sync(queue_b, ^{
        NSLog(@"B - 2");
    });

    dispatch_sync(queue_b, ^{
        NSLog(@"B - 3");
    });
}

But, surprisingly I always get the following output:

A - 1  
A - 2  
A - 3  
B - 1  
B - 2  
B - 3  

Why these two concurrent queues don't execute concurrently ?

Here is what Apple document says (reference):

Blocks submitted to a serial queue are executed one at a time in FIFO order. Note, however, that blocks submitted to independent queues may be executed concurrently with respect to each other. Blocks submitted to a concurrent queue are dequeued in FIFO order but may run concurrently if resources are available to do so.

So, as per this statement, in my case, the blocks of queue B should execute regardless of what's happening in queue A. Means, totally independent than queue A. But, why the blocks of queue B don't start their execution until all blocks of queue A finish.

Correct me if something has been misunderstood.

Upvotes: 1

Views: 2368

Answers (1)

Catfish_Man
Catfish_Man

Reputation: 41801

dispatch_sync means "wait until this is done before continuing". So you run one block, wait, run another block, wait, and so on. You have to use dispatch_async to get concurrency.

(edit) also there's no reason to create a second concurrent queue here, one will have the same effect

Upvotes: 6

Related Questions