首页 > dispatch_queue_create(com.biostime.xxx, DISPATCH_QUEUE_SERIAL)的陷阱

dispatch_queue_create(com.biostime.xxx, DISPATCH_QUEUE_SERIAL)的陷阱

代码

for(int i =0;i<10;i++)

    {

        NSLog(@"i=%d",i);

        dispatch_queue_t mySerialQueue = dispatch_queue_create("com.biostime.xxx", DISPATCH_QUEUE_SERIAL);

        __block int d = i;

        dispatch_async(mySerialQueue, ^{

            NSRunLoop *loop = [NSRunLoop currentRunLoop];

            NSLog(@"%p excute1 i = %d d=%d",loop,i,d);

            d=9;

            NSLog(@"%p excute2 i = %d d=%d",loop,i,d);

        });

        NSLog(@"i=%d",i);

    }

 

=============

2015-05-29 14:38:49.322 GCDDemo[8558:405173] operation begin

2015-05-29 14:38:49.322 GCDDemo[8558:405173] for begin 0

2015-05-29 14:38:49.322 GCDDemo[8558:405173] mySerialQueue 0x7fb0b051d490

2015-05-29 14:38:49.322 GCDDemo[8558:405173] for end 0

2015-05-29 14:38:49.322 GCDDemo[8558:405173] for begin 1

2015-05-29 14:38:49.322 GCDDemo[8558:405173] mySerialQueue 0x7fb0b05835e0

2015-05-29 14:38:49.322 GCDDemo[8558:405173] for end 1

2015-05-29 14:38:49.322 GCDDemo[8558:405173] for begin 2

2015-05-29 14:38:49.323 GCDDemo[8558:405173] mySerialQueue 0x7fb0b05858b0

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for end 2

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for begin 3

2015-05-29 14:38:49.323 GCDDemo[8558:405208] {number = 2, name = (null)} === begin

2015-05-29 14:38:49.323 GCDDemo[8558:405209] {number = 3, name = (null)} === begin

2015-05-29 14:38:49.323 GCDDemo[8558:405207] {number = 4, name = (null)} === begin

2015-05-29 14:38:49.323 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0509480

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for end 3

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for begin 4

2015-05-29 14:38:49.323 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0516280

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for end 4

2015-05-29 14:38:49.323 GCDDemo[8558:405173] for begin 5

2015-05-29 14:38:49.323 GCDDemo[8558:405215] {number = 5, name = (null)} === begin

2015-05-29 14:38:49.323 GCDDemo[8558:405216] {number = 6, name = (null)} === begin

2015-05-29 14:38:49.337 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0610000

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for end 5

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for begin 6

2015-05-29 14:38:49.337 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0623fa0

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for end 6

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for begin 7

2015-05-29 14:38:49.337 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0612e10

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for end 7

2015-05-29 14:38:49.337 GCDDemo[8558:405173] for begin 8

2015-05-29 14:38:49.338 GCDDemo[8558:405173] mySerialQueue 0x7fb0b062a590

2015-05-29 14:38:49.338 GCDDemo[8558:405173] for end 8

2015-05-29 14:38:49.338 GCDDemo[8558:405173] for begin 9

2015-05-29 14:38:49.338 GCDDemo[8558:405217] {number = 7, name = (null)} === begin

2015-05-29 14:38:49.338 GCDDemo[8558:405173] mySerialQueue 0x7fb0b0626860

2015-05-29 14:38:49.338 GCDDemo[8558:405173] for end 9

2015-05-29 14:38:49.338 GCDDemo[8558:405218] {number = 8, name = (null)} === begin

2015-05-29 14:38:49.338 GCDDemo[8558:405173] operation end

2015-05-29 14:38:49.338 GCDDemo[8558:405219] {number = 9, name = (null)} === begin

2015-05-29 14:38:49.338 GCDDemo[8558:405220] {number = 10, name = (null)} === begin

2015-05-29 14:38:49.339 GCDDemo[8558:405221] {number = 11, name = (null)} === begin

2015-05-29 14:38:51.326 GCDDemo[8558:405208] {number = 2, name = (null)} === end

2015-05-29 14:38:51.326 GCDDemo[8558:405209] {number = 3, name = (null)} === end

2015-05-29 14:38:51.326 GCDDemo[8558:405207] {number = 4, name = (null)} === end

2015-05-29 14:38:51.339 GCDDemo[8558:405215] {number = 5, name = (null)} === end

2015-05-29 14:38:51.339 GCDDemo[8558:405216] {number = 6, name = (null)} === end

2015-05-29 14:38:51.343 GCDDemo[8558:405217] {number = 7, name = (null)} === end

2015-05-29 14:38:51.343 GCDDemo[8558:405220] {number = 10, name = (null)} === end

2015-05-29 14:38:51.343 GCDDemo[8558:405218] {number = 8, name = (null)} === end

2015-05-29 14:38:51.343 GCDDemo[8558:405221] {number = 11, name = (null)} === end

2015-05-29 14:38:51.344 GCDDemo[8558:405219] {number = 9, name = (null)} === end

 解析:

dispatch_queue_create,用DISPATCH_QUEUE_SERIAL参数创建的是串行队列。那么,只要id相同,返回的都是同一个queue吗?

答案是,错误。

从日志中可以看到,系统生成的10个子线程来执行任务。所以,10个block仍然是并行执行。

label的作用,文档中指出,仅仅为了在debug,instrument,crash report中进行线程标识。

所以,label并不是线程的unique id,而是可以多个线程共用同一个id。

 请不要被label所误导。

转载于:https://www.cnblogs.com/so-magic/p/4529388.html

更多相关: