热门搜索 :
考研考公
您的当前位置:首页正文

dispatch_semaphore使用

来源:东饰资讯网

想象种场景:1.要下载十个文件,但是同一时刻只想让两个文件处于下载状态,用
GCD如何实现?
这种问题涉及到对并发的控制,使用信号量就可以很好的控制。
信号量是一个整形值并且具有一个初始计数值,并且支持两个操作:信号通知和等待。当一个信号量被信号通知,其计数会被增加。当一个线程在一个信号量上等待时,线程会被阻塞(如果有必要的话),直至计数器大于零,然后线程会减少这个计数。
在GCD中有三个函数是关于semaphore的操作,分别是:

  dispatch_semaphore_create   创建一个semaphore
  dispatch_semaphore_signal   发送一个信号,使信号量+1
  dispatch_semaphore_wait    等待信号

不要看他们的样子很生疏,这个semaphore单词还不认识,感觉很晦涩,其实真正用的时候就会发现还是很简单的。这套API简单到就3个函数,函数的参数一般也就一两个。
信号量可以拿停车场的例子来类比,方便理解。
假如有一个停车场有3个空车位,停车场外面有个信号灯显示着有3个空位,这时停车场外面来了五辆车,前三量车依次进入停车场,每进入一辆车,信号灯都要减1,当信号灯减成0的时候就意味着没有车位了,外面的车只能等待,不得进入停车场。 而当一辆车开走的时候,信号灯上又会加1,意味着有空车位了,就可以有一辆车进入。dispatch_queue_create就是创建信号,dispatch_semaphore_wait就是在停车场外鸣笛,准备进入停车场,此时如果有空余车外,可以顺利进入,并让信号灯减1,加入没有车位,就一直在外面等车,直到信号灯数量大于1时候就马上进入 dispatch_semaphore_signal就是让信号灯数量+1,如果外面有等待的车辆, 就告诉它可以进来了。

一 .API介绍

1.dispatch_semaphore_create

/*!
 * @function dispatch_semaphore_create
 *
 * @abstract
 * 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.
 * 传0用于 两个线程需要以特定顺序完成某个事件,传大于0的数用于管理资源,资源池的资源数要和传入的值一致
 * @param value
 * The starting value for the semaphore. Passing a value less than zero will
 * cause NULL to be returned.
 * 信号量初始值。传入低于0的值会返回NULL
 * @result
 * The newly created semaphore, or NULL on failure.
 */
dispatch_semaphore_t
dispatch_semaphore_create(long value);

2.dispatch_semaphore_wait

/*!
 * @function dispatch_semaphore_wait
 *
 * @abstract
 * Wait (decrement) for a semaphore.
 *  等待(降低)信号量
 * @discussion
 * Decrement the counting semaphore. If the resulting value is less than zero,
 * this function waits for a signal to occur before returning.
 * 降低信号量。如果返回值小于0,该函数会阻塞 等待直到信号发生才能返回
 * @param dsema
 * The semaphore. The result of passing NULL in this parameter is undefined.
 *  信号对象。传NULL会发生不确定结果
 * @param timeout
 * When to timeout (see dispatch_time). As a convenience, there are the
 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
 *  超时时间。为了方便,系统提供了两个常亮:DISPATCH_TIME_NOW和DISPATCH_TIME_FOREVER
 * @result
 * Returns zero on success, or non-zero if the timeout occurred.
 * 返回0代表成功,返回非0代表超时时间到达了
 */
long
dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);

3.dispatch_semaphore_signal

/*!
 * @function dispatch_semaphore_signal
 *
 * @abstract
 * 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.
 * 这个方法唤醒一个等待的线程 然后返回
 * @param dsema The counting semaphore.
 * The result of passing NULL in this parameter is undefined.
 *  传0是未定义的
 * @result
 * This function returns non-zero if a thread is woken. Otherwise, zero is
 * returned.
 *  返回非0代表有线程被唤醒。否则,返回0
 */
long
dispatch_semaphore_signal(dispatch_semaphore_t dsema);

二.应用

应用也很简单了

    //创建信号量  初始化为0
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    _semaphore = semaphore;
    
    for (int i = 0; i < 10; i ++ ) {
        
        dispatch_async(globalqueue, ^{
            
            //执行任务会使信号量减1,当执行两个任务后,信号量变为0,无法在继续执行。
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            sleep(3);
            NSLog(@"---------任务%d完成---%@",i,[NSThread currentThread]);
            //执行完任务后,使信号量加1,继续执行后面任务
            dispatch_semaphore_signal(semaphore);
            
        });
    }

使用for循环将十个任务添加到并行队列中,正常情况下,十个任务会同时执行。使用dispatch_semaphore就可以控制同一时刻只能有两个任务在执行。

dispatch_semaphore也可以控制两个任务的执行顺序,比如任务1 和任务2 ,创建一个出事信号量为0的semaphore对象,在任务1前面使用dispatch_semaphore_wait函数阻塞,等待信号量大于0 当任务2完成的时候发送信号,使任务1执行。 这样就可以控制任务2先完成,任务1后完成。

Top