문제

I am learning GCD Now. I create two global queues with different priority:

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);

I execute the two queues:

dispatch_async(q1, ^{

    [self task:@"q1"];

});

dispatch_async(q2, ^{

    [self task:@"q2"];

});

- (void)task:(NSString *)taskid
{
    for (int i = 0; i < 1000; i++) {

        NSLog(@"Now executing taskid:%@ num : %d", taskid, i);

        [NSThread sleepForTimeInterval:5];

    }
}

But the result shows that the two queues are executed concurrently, but not that the queue with higher priority executed firstly. So what does priority really means?

도움이 되었습니까?

해결책

From the docs for DISPATCH_QUEUE_PRIORITY_HIGH:

Items dispatched to the queue run at high priority; the queue is scheduled for execution before any default priority or low priority queue.

So code from different queues can still run concurrently, it's just that the higher priority queue is scheduled for execution before lower priority queues. Since the two queues may be setup in different threads, the two threads can run together. But it is quite possible that the higher priority queue (thread) will be given more opportunity to complete than the lower priority queue (thread).

It might be interesting to time how long (clock time) it take task: to run (try several iterations) and see if the higher priority queue gets done faster than lower priority queues.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top