Of course, the answer is in dispatch_queue_priority_t Constants, but the text is a bit misleading.
Why you don't see the behavior you expected…
This is a guess (and only a guess). You didn't crowd the CPU. In your test, the CPU has plenty of time to execute all the queues. No scheduler will run tasks based solely on priority. That leads to low priority tasks never executing. The scheduler picks a chooses tasks using priority as only one of the criteria.
If you setup a test with 100 or a 1000 concurrent DISPATCH_QUEUE_PRIORITY_DEFAULT and DISPATCH_QUEUE_PRIORITY_LOW tasks, you might start to see how the scheduler favors higher priority tasks.
UPDATE
Can't believe how wrong I was…
The answer is in dispatch_queue_priority_t Constants and it will alway pick higher priority queues over lower priority queues just like the document says.
Here is my version of the sample code:
dispatch_queue_t q1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t q2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
for (int i = 0; i < 50; ++i) {
dispatch_async(q1, ^{
for (int j = 0; j < 5; ++j) {
NSLog(@"Now executing %@[%d:%d]", @"DISPATCH_QUEUE_PRIORITY_DEFAULT", i, j);
[NSThread sleepForTimeInterval:0.00001];
}
});
}
for (int i = 0; i < 50; ++i) {
dispatch_async(q2, ^{
for (int j = 0; j < 5; ++j) {
NSLog(@"Now executing %@[%d:%d]", @"DISPATCH_QUEUE_PRIORITY_LOW", i, j);
[NSThread sleepForTimeInterval:0.00001];
}
});
}
The output:
2014-04-15 21:27:22.525 APP_NAME[10651:1103] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[0:0]
2014-04-15 21:27:22.526 APP_NAME[10651:1103] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[0:1]
2014-04-15 21:27:22.526 APP_NAME[10651:3803] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[1:0]
…
2014-04-15 21:27:22.810 APP_NAME[10651:3b03] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[47:3]
2014-04-15 21:27:22.812 APP_NAME[10651:3b03] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[47:4]
2014-04-15 21:27:22.812 APP_NAME[10651:3f03] Now executing DISPATCH_QUEUE_PRIORITY_DEFAULT[39:4]
2014-04-15 21:27:22.813 APP_NAME[10651:3d07] Now executing DISPATCH_QUEUE_PRIORITY_LOW[0:0]
2014-04-15 21:27:22.813 APP_NAME[10651:3f03] Now executing DISPATCH_QUEUE_PRIORITY_LOW[1:0]
2014-04-15 21:27:22.813 APP_NAME[10651:3d07] Now executing DISPATCH_QUEUE_PRIORITY_LOW[0:1]
…
2014-04-15 21:27:22.998 APP_NAME[10651:3d07] Now executing DISPATCH_QUEUE_PRIORITY_LOW[49:3]
2014-04-15 21:27:22.999 APP_NAME[10651:3f03] Now executing DISPATCH_QUEUE_PRIORITY_LOW[48:4]
2014-04-15 21:27:22.999 APP_NAME[10651:3d07] Now executing DISPATCH_QUEUE_PRIORITY_LOW[49:4]
No DISPATCH_QUEUE_PRIORITY_LOW
task was executed before a DISPATCH_QUEUE_PRIORITY_DEFAULT