I am trying to verify the functionality of dispatch_barrier_async
, and use "points of interest" to check what happened.
The code is as below:
_syncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int index = 0; index < 3; ++index) {
kdebug_signpost_start(0, 0, 0, 0, 0);
dispatch_sync(_syncQueue, ^{
NSLog(@"sync@@@@@@ >>>> %d ",index);
sleep(1);
NSLog(@"sync@@@@@@ <<<< %d ",index);
});
kdebug_signpost_end(0, 0, 0, 0, 0);
}
for (int index = 3; index < 6; ++index) {
kdebug_signpost_start(1, 0, 0, 0, 1);
dispatch_barrier_async(_syncQueue, ^{
NSLog(@"sync===== >>>> %d ",index);
sleep(1);
NSLog(@"sync===== <<<< %d ",index);
});
kdebug_signpost_end(1, 0, 0, 0, 1);
}
for (int index = 6; index < 9; ++index) {
kdebug_signpost_start(2, 0, 0, 0, 2);
dispatch_sync(_syncQueue, ^{
NSLog(@"sync***** >>>> %d ",index);
sleep(1);
NSLog(@"sync***** <<<< %d ",index);
});
kdebug_signpost_end(2, 0, 0, 0, 2);
}
I expect that
all tasks dispatched by
dispatch_barrier_async
(the 2nd loop) should work after the tasks which is executing finishes; andonce the tasks dispatched by
dispatch_barrier_async
start to work, no other task in same queue can be run.
But the result is as below:
sync@@@@@@ >>>> 0
sync@@@@@@ <<<< 0
sync@@@@@@ >>>> 1
sync@@@@@@ <<<< 1
sync@@@@@@ >>>> 2
sync@@@@@@ <<<< 2
sync***** >>>> 6
sync===== >>>> 3
sync===== >>>> 4
sync===== >>>> 5
sync***** <<<< 6
sync***** >>>> 7
sync===== <<<< 3
sync===== <<<< 4
sync===== <<<< 5
sync***** <<<< 7
sync***** >>>> 8
My confusion as below:
since task 6 is already started before task 3, and task 3 is
dispatch_barrier_async
dispatched task, why taks 3 does not wait task 6 finish and then start ? which violates my expectation a);after dispatch_barrier_async dispatched task 5 start to work, another task 7 is started before task 5 finished, which violates my expectation b)
I use "points of interest" tool to debug it, but seems, the 2nd loop does not shows on the graphic generated by intrument. it's my first time to use this tool, Could someone help to point out what's the issue here ?
[04/02] Thanks Rob. Here are some update after following your suggestion.
Update1: After change the queue creation method of 2nd loop to dispatch_queue_create, it works well. the tasks dispatched by dispatch_barrier_async will wait for the tasks before them to finish. and the tasks after them will start util they(tasks dispatched by dispatch_barrier_async) finished.
update2: Then I used the code you provided, and change the queue creation of 2nd loop back to dispatch_async, they also works as expection. But, the "points of interest" graphic has some problem. some task in 2nd loop cannot be displayed correctly.
Xcode indicate some error for (0x4 0x0 0x0 0x1) and (0x5 0x0 0x0 0x1), as "The interval can not be logged as an interval because the start could not be determined."
the (0x3 0x0 0x0 0x1) is not listed, but (0x5 0x0 0x0 0x1) is listed twice. and only one (0x5 0x0 0x0 0x1) displayed on the graphic
I never see an indicator Ⓢ in the "points of interest" tool.
That yields the following messages:
00:00.000.000 2ndLoop (KDebug Interval Signpost) ended by test.app (pid: 64903, tid: 0x1619027), with arguments: (0x4 0x0 0x0 0x1). The interval can not be logged as an interval because the start could not be determined. __kdebug_trace64 ← (8 other frames)
00:00.000.000 2ndLoop (KDebug Interval Signpost) ended by test.app (pid: 64903, tid: 0x1619028), with arguments: (0x5 0x0 0x0 0x1). The interval can not be logged as an interval because the start could not be determined. __kdebug_trace64 ← (8 other frames)
00:04.134.364 2ndLoop (KDebug Interval Signpost), started by test.app (pid: 64903, tid: 0x1619028), with arguments: (0x5 0x0 0x0 0x1), ended 1.00 s later by test.app (pid: 64903, tid: 0x1619026), with arguments: (0x3 0x0 0x0 0x1) __kdebug_trace64 ← (9 other frames)