GCD使用(六)串行队列的构造

来源:互联网 发布:c语言随机数的产生 编辑:程序博客网 时间:2024/05/16 00:47

1。串行队列:

      FIFO方式,并发时的异步操作不会在主线程进行 

   dispatch_queue_create(CStringID , 0): CStringID形式-com.COMPANY.PRODUCT.ID,不需要加@

e.g.

    //串行队列,非mainQueue,非globalQueue

    dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0);

    

    //虽然是异步操作,但仍是FIFO,因为是在自己构造的串行队列中执行

    dispatch_async(firstSerialQueue, ^{

        NSUInteger counter = 0;

        for (counter = 0;

             counter < 5;

             counter++){

            NSLog(@"First iteration, counter = %lu", (unsigned long)counter);

        }

    });    

    dispatch_async(firstSerialQueue, ^{

        NSUInteger counter = 0;

        for (counter = 0;

             counter < 5;

             counter++){

            NSLog(@"Second iteration, counter = %lu", (unsigned long)counter);

        }

    });


    dispatch_async(firstSerialQueue, ^{

        NSUInteger counter = 0;

        for (counter = 0;

             counter < 5;

             counter++){

            NSLog(@"Third iteration, counter = %lu", (unsigned long)counter);

        }

    });

e.g.

  C函数

void firstIteration(void *paramContext){ 

    NSUInteger counter = 0;

    for (counter = 0;

         counter < 5;

         counter++){

        NSLog(@"%@ iteration, counter = %lu",paramContext, (unsigned long)counter);

    }

}

- (void) actionSerial{  

    dispatch_queue_t firstSerialQueue =  dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0);

    dispatch_async_f(firstSerialQueue, (__bridge void *)( @"First"), firstIteration);

    dispatch_async_f(firstSerialQueue, (__bridge void *)( @"Second"), firstIteration);

    dispatch_async_f(firstSerialQueue, (__bridge void *)( @"Third"), firstIteration);

}



0 0
原创粉丝点击