GCD全解-07-dispatch_semaphore

版权声明:知识版权是属于全人类的! 欢迎评论与转载!!! https://blog.csdn.net/zhuge1127/article/details/82464858

主要API列表
dispatch_semaphore_create   创建一个semaphore
dispatch_semaphore_signal   发送一个信号
dispatch_semaphore_wait    等待信号

如何在GCD中快速的控制并发呢?
信号量是一个整形值并且具有一个初始计数值,并且支持两个操作:信号通知和等待。
当一个信号量被信号通知,其计数会被增加。当一个线程在一个信号量上等待时,线程会被阻塞(如果有必要的话),直至计数器大于零,然后线程会减少这个计数。

dispatch_semaphore_create

 Creates new counting semaphore with an initial value.
 *
 * @discussion
 * Passing zero for the value is useful for when two threads need to reconcile
 * the completion of a particular event. Passing a value greater than zero is
 * useful for managing a finite pool of resources, where the pool size is equal
 * to the value.
 *
 * @param value
 * The starting value for the semaphore. Passing a value less than zero will
 * cause NULL to be returned.

dispatch_semaphore_t dispatch_semaphore_create(long value);

dispatch_semaphore_wait

Wait (decrement) for a semaphore.
    当信号总量少于0的时候就会一直等待,否则就可以正常的执行,并让信号总量-1,
 * @discussion
 * Decrement the counting semaphore. If the resulting value is less than zero,
 * this function waits for a signal to occur before returning.
 *
 * @param timeout
 * When to timeout (see dispatch_time). As a convenience, there are the
 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.

long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);

dispatch_semaphore_signal

Signal (increment) a semaphore.
 * @discussion
 * Increment the counting semaphore. If the previous value was less than zero,
 * this function wakes a waiting thread before returning.

long dispatch_semaphore_signal(dispatch_semaphore_t dsema);

demo

为了确保 信号先发送再接受,
就可以使用信号量创建的时候为0, 表示当前的资源不可使用,
当并发执行第二个block的时候发现需要等待信号量为1. 所以暂时线程不执行
而第一个block是不需要等待信号的.于是先执行第一个block,并且发送一个信号.于是第二个等待执行的block会执行.
此时因为当前第一个block尚未执行完毕,那么此时两个block将会交替执行
换句话说: 如果block发送信号注释掉不执行 dispatch_semaphore_signal(semaphore);那么block2就不会打印信号已接受就没有下文了

- (void)viewWillAppear:(BOOL)animated {
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"信号已发送");
        dispatch_semaphore_signal(semaphore);
        for (int i = 0; i < 10; i++) {
            NSLog(@"%i", i);
        }
    });
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"信号已接受");
        for (int i = 0; i < 10; i++) {
            NSLog(@"第二个block%i", i);
        }
    });
}


2016-11-05 16:41:34.529 Multithreading[15864:468169] 信号已发送
2016-11-05 16:41:34.529 Multithreading[15864:468185] 信号已接受
2016-11-05 16:41:34.529 Multithreading[15864:468169] 0
2016-11-05 16:41:34.529 Multithreading[15864:468185] 第二个block0
2016-11-05 16:41:34.529 Multithreading[15864:468185] 第二个block1
2016-11-05 16:41:34.529 Multithreading[15864:468169] 1
2016-11-05 16:41:34.529 Multithreading[15864:468185] 第二个block2
2016-11-05 16:41:34.530 Multithreading[15864:468169] 2
2016-11-05 16:41:34.530 Multithreading[15864:468185] 第二个block3
2016-11-05 16:41:34.530 Multithreading[15864:468169] 3
2016-11-05 16:41:34.530 Multithreading[15864:468185] 第二个block4
2016-11-05 16:41:34.530 Multithreading[15864:468169] 4

猜你喜欢

转载自blog.csdn.net/zhuge1127/article/details/82464858
今日推荐