并发数据顺序执行

dispatch_semaphore_t sem = dispatch_semaphore_create(1);

dispatch_queue_t queue = dispatch_queue_create("testBlock", NULL);

dispatch_async(queue, ^{

dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

NSLog(@"1");

dispatch_semaphore_signal(sem);

});

});

dispatch_async(queue, ^{

dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

NSLog(@"2");

dispatch_semaphore_signal(sem);

});

dispatch_async(queue, ^{

dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

NSLog(@"3");

dispatch_semaphore_signal(sem);

});

});

dispatch_async(queue, ^{

dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

NSLog(@"4");

dispatch_semaphore_signal(sem);

});


著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

你可能感兴趣的:(并发数据顺序执行)