在做视频渲染时,控制视频播放速度时使用gcd和usleep时发现一个奇怪的现象。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self abc];
});
- (void)abc {
///获取当前时间
long lastTime = getTickCount();
while (YES) {
usleep(30 * 1000);
long t = getTickCount();
NSLog(@"==== space = %ld", t - lastTime);
lastTime = t;
}
}
在队列的优先级用
DISPATCH_QUEUE_PRIORITY_LOW和DISPATCH_QUEUE_PRIORITY_BACKGROUND时,
打印结果如下
2018-10-15 10:13:11.222745+0800 Test1[961:16849] ==== space = 103
2018-10-15 10:13:11.324761+0800 Test1[961:16849] ==== space = 102
2018-10-15 10:13:11.425194+0800 Test1[961:16849] ==== space = 101
2018-10-15 10:13:11.528114+0800 Test1[961:16849] ==== space = 103
2018-10-15 10:13:11.633357+0800 Test1[961:16849] ==== space = 105
2018-10-15 10:13:11.731285+0800 Test1[961:16849] ==== space = 98
2018-10-15 10:13:11.834159+0800 Test1[961:16849] ==== space = 103
2018-10-15 10:13:11.936076+0800 Test1[961:16849] ==== space = 102
2018-10-15 10:13:12.037427+0800 Test1[961:16849] ==== space = 101
2018-10-15 10:13:12.139619+0800 Test1[961:16849] ==== space = 102
在队列的优先级用
DISPATCH_QUEUE_PRIORITY_DEFAULT和DISPATCH_QUEUE_PRIORITY_HIGH时,
打印结果如下
2018-10-15 10:23:30.333045+0800 Test1[1246:26463] ==== space = 32
2018-10-15 10:23:30.363838+0800 Test1[1246:26463] ==== space = 30
2018-10-15 10:23:30.397944+0800 Test1[1246:26463] ==== space = 34
2018-10-15 10:23:30.432868+0800 Test1[1246:26463] ==== space = 35
2018-10-15 10:23:30.465935+0800 Test1[1246:26463] ==== space = 33
2018-10-15 10:23:30.501469+0800 Test1[1246:26463] ==== space = 36
2018-10-15 10:23:30.536906+0800 Test1[1246:26463] ==== space = 35
2018-10-15 10:23:30.571026+0800 Test1[1246:26463] ==== space = 35
2018-10-15 10:23:30.601909+0800 Test1[1246:26463] ==== space = 30
2018-10-15 10:23:30.637200+0800 Test1[1246:26463] ==== space = 36
2018-10-15 10:23:30.669920+0800 Test1[1246:26463] ==== space = 32
2018-10-15 10:23:30.704644+0800 Test1[1246:26463] ==== space = 35
2018-10-15 10:23:30.737739+0800 Test1[1246:26463] ==== space = 33
2018-10-15 10:23:30.772425+0800 Test1[1246:26463] ==== space = 35
为什么会产生这样的结果暂时不知,这里做一个笔记。