美文网首页IOS知识积累多线程等ios
iOS dispatch_semaphore是GCD用来同步的一

iOS dispatch_semaphore是GCD用来同步的一

作者: 石虎132 | 来源:发表于2017-05-29 22:35 被阅读212次

    联系人:石虎QQ: 1224614774  昵称:嗡嘛呢叭咪哄

    dispatch_semaphore是GCD用来同步的一种方式,与他相关的共有三个函数,分别是

    dispatch_semaphore_create,

    dispatch_semaphore_signal,

    dispatch_semaphore_wait。

    下面我们逐一介绍三个函数:

    1.dispatch_semaphore_create函数的声明为:

    dispatch_samaphore_tdispatch_semaphore_create(longvalue);  传入的参数为long,输出一个dispatch_semaphore_t类型且值为value的信号量。    值得注意的是,这里的传入的参数value必须大于或等于0,否则dispatch_semaphore_create会返回NULL。

    (关于信号量,我就不在这里累述了,网上很多介绍这个的。我们这里主要讲一下dispatch_semaphore这三个函数的用法)。

    2.dispatch_semaphore_signal的声明为:

    long dispatch_semaphore_signal(dispatch_semaphore_tdsema)

    这个函数会使传入的信号量dsema的值加1;(至于返回值,待会儿再讲)

    3. dispatch_semaphore_wait的声明为

    longdispatch_semaphore_wait(dispatch_semaphore_tdsema,dispatch_time_ttimeout);

    这个函数会使传入的信号量dsema的值减1;

    这个函数的作用是这样的,如果dsema信号量的值大于0,该函数所处线程就继续执行下面的语句,并且将信号量的值减1;

    如果desema的值为0,那么这个函数就阻塞当前线程等待timeout(注意timeout的类型为dispatch_time_t,

    不能直接传入整形或float型数),如果等待的期间desema的值被dispatch_semaphore_signal函数加1了,

    且该函数(即dispatch_semaphore_wait)所处线程获得了信号量,那么就继续向下执行并将信号量减1。

    如果等待期间没有获取到信号量或者信号量的值一直为0,那么等到timeout时,其所处线程自动执行其后语句。

    4.dispatch_semaphore_signal的返回值为long类型

    当返回值为0时表示当前并没有线程等待其处理的信号量,其处理

    的信号量的值加1即可。当返回值不为0时,表示其当前有(一个或多个)线程等待其处理的信号量,并且该函数唤醒了一个等待的线程(当线程有优先级时,唤醒优先级最高的线程;否则随机唤醒)。

    dispatch_semaphore_wait的返回值也为long型。当其返回0时表示在timeout之前,该函数所处的线程被成功唤醒。

    当其返回不为0时,表示timeout发生。

    5.在设置timeout时,比较有用的两个宏:DISPATCH_TIME_NOW 和 DISPATCH_TIME_FOREVER。

    DISPATCH_TIME_NOW表示当前;DISPATCH_TIME_FOREVER表示遥远的未来;

    一般可以直接设置timeout为这两个宏其中的一个,或者自己创建一个dispatch_time_t类型的变量。

    创建dispatch_time_t类型的变量有两种方法,dispatch_time和dispatch_walltime。

    利用创建dispatch_time创建dispatch_time_t类型变量的时候一般也会用到这两个变量。

    dispatch_time的声明如下:

    dispatch_time_t dispatch_time(dispatch_time_t when, int64_t delta);

    其参数when需传入一个dispatch_time_t类型的变量,和一个delta值。表示when加delta时间就是timeout的时间。

    例如:dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, 110001000*1000);

    表示当前时间向后延时一秒为timeout的时间。

    6.关于信号量,一般可以用停车来比喻。

    停车场剩余4个车位,那么即使同时来了四辆车也能停的下。如果此时来了五辆车,那么就有一辆需要等待。

    信号量的值就相当于剩余车位的数目,dispatch_semaphore_wait函数就相当于来了一辆车,dispatch_semaphore_signal

    就相当于走了一辆车。停车位的剩余数目在初始化的时候就已经指明了(dispatch_semaphore_create(long value)),

    调用一次dispatch_semaphore_signal,剩余的车位就增加一个;调用一次dispatch_semaphore_wait剩余车位就减少一个;

    当剩余车位为0时,再来车(即调用dispatch_semaphore_wait)就只能等待。有可能同时有几辆车等待一个停车位。有些车主

    没有耐心,给自己设定了一段等待时间,这段时间内等不到停车位就走了,如果等到了就开进去停车。而有些车主就像把车停在这,

    所以就一直等下去。

    7.代码举简单示例如下:

    dispatch_semaphore_tsignal;signal= dispatch_semaphore_create(1);__block long x =0;NSLog(@"0_x:%ld",x);dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{    sleep(1);    NSLog(@"waiting");    x = dispatch_semaphore_signal(signal);    NSLog(@"1_x:%ld",x);    sleep(2);    NSLog(@"waking");    x = dispatch_semaphore_signal(signal);    NSLog(@"2_x:%ld",x);});//    dispatch_time_t duration = dispatch_time(DISPATCH_TIME_NOW, 1*1000*1000*1000); //超时1秒//    dispatch_semaphore_wait(signal, duration);x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);NSLog(@"3_x:%ld",x);x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);NSLog(@"wait 2");NSLog(@"4_x:%ld",x);x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);NSLog(@"wait 3");NSLog(@"5_x:%ld",x);

    最终打印的结果为:

    2014-08-1122:51:54.734LHTest[15700:70b]0_x:02014-08-1122:51:54.737LHTest[15700:70b]3_x:02014-08-1122:51:55.738LHTest[15700:f03]waiting2014-08-1122:51:55.739LHTest[15700:70b]wait2    2014-08-1122:51:55.739LHTest[15700:f03]1_x:12014-08-1122:51:55.739LHTest[15700:70b]4_x:02014-08-1122:51:57.741LHTest[15700:f03]waking2014-08-1122:51:57.742LHTest[15700:f03]2_x:12014-08-1122:51:57.742LHTest[15700:70b]wait3    2014-08-1122:51:57.742LHTest[15700:70b]5_x:0

    相关文章

      网友评论

      本文标题:iOS dispatch_semaphore是GCD用来同步的一

      本文链接:https://www.haomeiwen.com/subject/zeylfxtx.html