iOS学习笔记74-完整详解GCD系列(四)dispatch_semaphore(信号量)

原创Blog,转载请注明出处

http://blog.csdn.net/hello_hwc?viewmode=contents


一 何为信号量?

  简单来说就是控制访问资源的数量,比如系统有两个资源可以被利用,同时有三个线程要访问,只能允许两个线程访问,第三个应当等待资源被释放后再访问。

注意:再GCD中,只有调度的线程在信号量不足的时候才会进入内核态进行线程阻塞

二 如何使用信号量

三个主要函数

创建一个信号量

[plain]  view plain  copy
  1. func dispatch_semaphore_create(_ value: Int) ->  dispatch_semaphore_t !  
其中value为信号量的初值,如果小于0则会返回NULL


提高信号量

[plain]  view plain  copy
  1. func dispatch_semaphore_signal(_ dsema: dispatch_semaphore_t!) -> Int  

等待降低信号量

[plain]  view plain  copy
  1. func dispatch_semaphore_wait(_ dsema: dispatch_semaphore_t!,  
  2.                            _ timeout: dispatch_time_t) -> Int  
注意,正常的使用顺序是先降低然后再提高,这两个函数通常成对使用。

三 举例分析

[plain]  view plain  copy
  1. <pre name="code" class="objc">//  
  2. //  ViewController.swift  
  3. //  SwiftTestExample  
  4. //  
  5. //  Created by huangwenchen on 15/1/6.  
  6. //  Copyright (c) 2015年 huangwenchen. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class ViewController: UIViewController {  
  12.     var semaphore:dispatch_semaphore_t;  
  13.     required init(coder aDecoder: NSCoder) {  
  14.         self.semaphore = dispatch_semaphore_create(1)  
  15.         super.init(coder: aDecoder)  
  16.     }  
  17.     override func viewDidLoad() {  
  18.         super.viewDidLoad()  
  19.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {() -> Void  in  
  20.              self.task_first()  
  21.         })  
  22.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in  
  23.             self.task_second()  
  24.         })  
  25.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in  
  26.             self.task_third()  
  27.         })  
  28.         // Do any additional setup after loading the view, typically from a nib.  
  29.     }  
  30.     func task_first(){  
  31.         dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)  
  32.         NSLog("%@","First task starting")  
  33.         sleep(1)  
  34.         NSLog("%@", "First task is done")  
  35.         dispatch_semaphore_signal(self.semaphore)  
  36.     }  
  37.     func task_second(){  
  38.         dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)  
  39.         NSLog("%@","Second task starting")  
  40.         sleep(1)  
  41.         NSLog("%@", "Second task is done")  
  42.         dispatch_semaphore_signal(self.semaphore)  
  43.     }  
  44.     func task_third(){  
  45.         dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)  
  46.         NSLog("%@","Thrid task starting")  
  47.         sleep(1)  
  48.         NSLog("%@", "Thrid task is done")  
  49.         dispatch_semaphore_signal(self.semaphore)  
  50.     }  
  51.     override func didReceiveMemoryWarning() {  
  52.         super.didReceiveMemoryWarning()  
  53.         // Dispose of any resources that can be recreated.  
  54.     }  
  55.   
  56.   
  57. }  

 
这段代码模拟提交三个任务,提交到全局队列(并行队列) 

当信号量的初初始为2时候

输出

[objc]  view plain  copy
  1. 2015-01-06 19:42:01.963 SwiftTestExample[632:11631] First task starting  
  2. 2015-01-06 19:42:01.964 SwiftTestExample[632:11630] Second task starting  
  3. 2015-01-06 19:42:02.971 SwiftTestExample[632:11630] Second task is done  
  4. 2015-01-06 19:42:02.971 SwiftTestExample[632:11631] First task is done  
  5. 2015-01-06 19:42:02.971 SwiftTestExample[632:11633] Thrid task starting  
  6. 2015-01-06 19:42:03.974 SwiftTestExample[632:11633] Thrid task is done  
当信号量为3的时候

[objc]  view plain  copy
  1. 2015-01-06 19:42:49.912 SwiftTestExample[666:12259] First task starting  
  2. 2015-01-06 19:42:49.912 SwiftTestExample[666:12258] Second task starting  
  3. 2015-01-06 19:42:49.912 SwiftTestExample[666:12260] Thrid task starting  
  4. 2015-01-06 19:42:50.915 SwiftTestExample[666:12259] First task is done  
  5. 2015-01-06 19:42:50.915 SwiftTestExample[666:12260] Thrid task is done  
  6. 2015-01-06 19:42:50.915 SwiftTestExample[666:12258] Second task is done  

当信号量为1的时候

[objc]  view plain  copy
  1. 2015-01-06 19:43:35.140 SwiftTestExample[694:12768] First task starting  
  2. 2015-01-06 19:43:36.145 SwiftTestExample[694:12768] First task is done  
  3. 2015-01-06 19:43:36.145 SwiftTestExample[694:12771] Second task starting  
  4. 2015-01-06 19:43:37.146 SwiftTestExample[694:12771] Second task is done  
  5. 2015-01-06 19:43:37.146 SwiftTestExample[694:12769] Thrid task starting  
  6. 2015-01-06 19:43:38.150 SwiftTestExample[694:12769] Thrid task is done 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值