iOSにおけるマルチスレッドのGCD応用
3065 ワード
GCD Grand Central Dispatch , 。 :
GCDの一般的な方法の紹介:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
});
dispatch_async(dispatch_get_main_queue(), ^{
//
});
static dispatch_once_t once;
dispatch_once(&once, ^{
//
});
float delaySeconds = 3.0f;
dispatch_time_t disTime = dispatch_time(DISPATCH_TIME_NOW, delaySeconds * NSEC_PER_SEC);
dispatch_after(disTime, dispatch_get_main_queue(), ^{
//
});
dispatch_queue_t serialQueue = dispatch_queue_create("queueName", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
//code
NSLog(@" : ");
});
dispatch_async(serialQueue, ^{
//code
NSLog(@" : ");
});
dispatch_queue_t concurrentQueue = dispatch_queue_create("queueName", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
//code
NSLog(@" : ");
});
dispatch_async(concurrentQueue, ^{
//code
NSLog(@" : ");
});
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"notify: ");
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"notify: ");
});
// group
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"notify: ");
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"notify: ");
});
dispatch_queue_t conCurrentQueue = dispatch_queue_create("conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(conCurrentQueue, ^{
//
NSLog(@"barrier: ");
});
dispatch_async(conCurrentQueue, ^{
//
NSLog(@"barrier: ");
});
dispatch_barrier_async(conCurrentQueue, ^{
//
NSLog(@"barrier: ");
});
dispatch_async(conCurrentQueue, ^{
//
NSLog(@"barrier: ");
});
dispatch_async(conCurrentQueue, ^{
//
NSLog(@"barrier: ");
});
後で他の適用シーンがあれば、補足します.よく書けないところがあれば、ご指摘ください.