GCD API 总结(原创)

Dispatch Queue即 执行处理的等待队列。

存在两种Dispatch Queue,即

Serial Dispatch Queue 等待现在的处理

Concurrent Dispatch Queue 不等待现在的处理

 

1.dispatch_queue_create("com.gcd.test",Null)

创建Dispatch queue,

DISPATCH_QUEUE_SERIAL

DISPATCH_QUEUE_CONCURRENT

- (void)tsGCD

{

    dispatch_queue_t queue = dispatch_queue_create("com.gcd.test", DISPATCH_QUEUE_CONCURRENT);

    NSMutableArray *array = [[NSMutableArray alloc]init];

    for (int i = 0; i < 100000; i++) {

        dispatch_async(queue, ^{

            [array addObject:[NSNumber numberWithInt:i]];

        });

    }

    NSLog(@"done");

}

 

当为DISPATCH_QUEUE_CONCURRENT,数组AddObject非线程安全,同时访问容易引起问题,

gcdTest(2484,0x11395b000) malloc: *** error for object 0x7f9c8b802e00: pointer being freed was not allocated

*** set a breakpoint in malloc_error_break to debug

当为DISPATCH_QUEUE_SERIAL时,线程有序进行,不会引起并发问题。

GCD API 总结(原创)

上一篇:8.3.3.1 C# 中的组合行为


下一篇:debug没有问题,release后出错(C#对excel处理)