Why the output of dispatch queue in sample does not match with code

Hi Experts,


I created a simple deadlock program with dispatch concurrent queue but no label:


dispatch_queue_t queueA = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queueA, ^(){

NSLog(@"3:%@", [NSThread currentThread]);

dispatch_barrier_sync(queueA, ^(){

NSLog(@"4:%@", [NSThread currentThread]);

});

});


the output of sample shows the dispatch queue is serial, not concurrent.


2733 Thread_<multiple> DispatchQueue_25: (null) (serial)

2733 start_wqthread (in libsystem_pthread.dylib) + 13 [0x1003bce01]

2733 _pthread_wqthread (in libsystem_pthread.dylib) + 583 [0x1003bd0b7]

2733 _dispatch_worker_thread2 (in libdispatch.dylib) + 125 [0x1003572da]

2733 _dispatch_root_queue_drain (in libdispatch.dylib) + 334 [0x100356824]

2733 _dispatch_async_redirect_invoke (in libdispatch.dylib) + 769 [0x1003463b4]

2733 _dispatch_continuation_pop (in libdispatch.dylib) + 563 [0x1003472be]

2733 _dispatch_client_callout (in libdispatch.dylib) + 8 [0x100343f1b]

2733 _dispatch_call_block_and_release (in libdispatch.dylib) + 12 [0x100342e7c]

2733 __main_block_invoke_2.14 (in learn_oc) + 125 [0x10000176d] main.mm:249

2733 _dispatch_sync_f_slow (in libdispatch.dylib) + 223 [0x10035346c]

2733 __DISPATCH_WAIT_FOR_QUEUE__ (in libdispatch.dylib) + 301 [0x100353c90]

2733 _dispatch_thread_event_wait_slow (in libdispatch.dylib) + 33 [0x1003448ec]

2733 _dispatch_ulock_wait (in libdispatch.dylib) + 47 [0x10034480d]

2733 __ulock_wait (in libsystem_kernel.dylib) + 10 [0x7fff7e3e19de]


But if I create the same concurrent queue with the label, the result is correct:

dispatch_queue_t queueA = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);


2891 Thread_34199 DispatchQueue_25: test (concurrent)

+ 2891 start_wqthread (in libsystem_pthread.dylib) + 13 [0x1003bce01]

+ 2891 _pthread_wqthread (in libsystem_pthread.dylib) + 583 [0x1003bd0b7]

+ 2891 _dispatch_worker_thread2 (in libdispatch.dylib) + 125 [0x1003572da]

+ 2891 _dispatch_root_queue_drain (in libdispatch.dylib) + 334 [0x100356824]

+ 2891 _dispatch_async_redirect_invoke (in libdispatch.dylib) + 769 [0x1003463b4]

+ 2891 _dispatch_continuation_pop (in libdispatch.dylib) + 563 [0x1003472be]

+ 2891 _dispatch_client_callout (in libdispatch.dylib) + 8 [0x100343f1b]

+ 2891 _dispatch_call_block_and_release (in libdispatch.dylib) + 12 [0x100342e7c]

+ 2891 __main_block_invoke_2.15 (in learn_oc) + 125 [0x10000176d] main.mm:249

+ 2891 _dispatch_sync_f_slow (in libdispatch.dylib) + 223 [0x10035346c]

+ 2891 __DISPATCH_WAIT_FOR_QUEUE__ (in libdispatch.dylib) + 301 [0x100353c90]

+ 2891 _dispatch_thread_event_wait_slow (in libdispatch.dylib) + 33 [0x1003448ec]

+ 2891 _dispatch_ulock_wait (in libdispatch.dylib) + 47 [0x10034480d]

+ 2891 __ulock_wait (in libsystem_kernel.dylib) + 10 [0x7fff7e3e19de]


It seems like if there is a label in the queue, that means you want to uniquely identify it in debugging tools such as Instruments,

sample
, stackshots, and crash reports, or else the system somehow would change the queue?

Why do you care?

I have a theory as to what’s going on here but I’m reluctant to share it here. This behaviour is very much tied to the internal implementation of Dispatch, so writing code that relies on it would be a mistake.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, eskimo.

Because I need to analyze the sample log of Apps, such as deadlock..., it's confusing me if the info of dispatch queue is incorrect.


I don’t think the “info of dispatch queue is incorrect”. Rather, you haven’t constrained the queue that will run your block and thus the system is free to run it on whatever queue is available. If you apply a constraint — for example, by setting a name — the system runs it on a queue with that constraint in place.

Are you using concurrent queues in your real app? If so, that’s probably a mistake. WWDC 2017 Session 706 Modernizing Grand Central Dispatch Usage explain why.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

A big thank for your clarification, eskimo.

Yes, I'm using concurrent queues in my App without label, so far the tasks are running concurrently, however, they may run serially according to you description above.

Anyway, I have to set label for concurrent queue in order to guarantee it works as expected alway?

Why the output of dispatch queue in sample does not match with code
 
 
Q