GCD NSOperationQueue线程依赖

来源:互联网 发布:网站域名备案怎么弄 编辑:程序博客网 时间:2024/06/01 10:15

线程依赖

http://blog.csdn.net/xgb742951920/article/details/52641133

//线程加锁

http://blog.csdn.net/likendsl/article/details/8568961

GCD是一个比较强大的线程处理机制,说白了也就是对队列的处理,一个队列可以并发出多个线程

http://www.th7.cn/Program/IOS/201503/416846.shtml

NSOpreationv 是添加依赖

GCD 是增加信号量

        @autoreleasepool {

            //创建信号量

            __blockdispatch_semaphore_t sem = dispatch_semaphore_create(0);

            __blockdispatch_semaphore_t sem1 = dispatch_semaphore_create(0);

            //创建一个队列

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

            

            //开启一个异步线程

            dispatch_async(queue, ^{

                

                for (int a=0; a<5; a++) {

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

                }

                //发送信号量

                dispatch_semaphore_signal(sem);

            });

            

            

            

            //接受信号量

            dispatch_semaphore_wait(sem,DISPATCH_TIME_FOREVER);

            dispatch_async(queue, ^{

                for (int b=0; b<5; b++) {

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

                }

                dispatch_semaphore_signal(sem1);

            });

       

            dispatch_semaphore_wait(sem1,DISPATCH_TIME_FOREVER);

            dispatch_async(queue, ^{

                for (int c=0; c<5; c++) {

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

                }

//                dispatch_semaphore_signal(sem1);

            });

        }


0 0
原创粉丝点击