完整详解swift GCD系列四dispatch_semaphore信号量

原创Blog,转载请注明出处

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


一 何为信号量?

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

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

二 如何使用信号量

三个主要函数

创建一个信号量

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


提高信号量

copy

    funcdispatch_semaphore_signal(_dsema:dispatch_semaphore_t!)->Int

等待降低信号量

copy

    funcdispatch_semaphore_wait(_dsema:dispatch_semaphore_t!,
  1. _timeout:dispatch_time_t)->Int
注意,正常的使用顺序是先降低然后再提高,这两个函数通常成对使用。

三 举例分析

copy
    <prename="code"class="objc">//
  1. //ViewController.swift
  2. //SwiftTestExample
  3. //
  4. //Createdbyhuangwenchenon15/1/6.
  5. //copyright(c)2015年huangwenchen.Allrightsreserved.
  6. //
  7. importUIKit
  8. classViewController:UIViewController{
  9. varsemaphore:dispatch_semaphore_t;
  10. requiredinit(coderaDecoder:NSCoder){
  11. self.semaphore=dispatch_semaphore_create(1)
  12. super.init(coder:aDecoder)
  13. }
  14. overridefuncviewDidLoad(){
  15. super.viewDidLoad()
  16. dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAULT,0),{()->Voidin
  17. self.task_first()
  18. })
  19. dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAULT,{()->Voidin
  20. self.task_second()
  21. })
  22. self.task_third()
  23. //Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
  24. }
  25. functask_first(){
  26. dispatch_semaphore_wait(self.semaphore,disPATCH_TIME_FOREVER)
  27. NSLog("%@","Firsttaskstarting")
  28. sleep(1)
  29. irsttaskisdone")
  30. dispatch_semaphore_signal(self.semaphore)
  31. functask_second(){
  32. dispatch_semaphore_wait(self.semaphore,disPATCH_TIME_FOREVER)
  33. NSLog("%@","Secondtaskstarting")
  34. sleep(1)
  35. dispatch_semaphore_signal(self.semaphore)
  36. functask_third(){
  37. overridefuncdidReceiveMemoryWarning(){
  38. super.didReceiveMemoryWarning()
  39. //dispoSEOfanyresourcesthatcanberecreated.
  40. }

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

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

输出

[objc] copy
    2015-01-0619:42:01.963SwiftTestExample[632:11631]Firsttaskstarting
  1. 2015-01-0619:42:01.964SwiftTestExample[632:11630]Secondtaskstarting
  2. 19:42:02.971SwiftTestExample[632:11630]Secondtaskisdone
  3. 19:42:02.971SwiftTestExample[632:11631]Firsttaskisdone
  4. 19:42:02.971SwiftTestExample[632:11633]Thridtaskstarting
  5. 19:42:03.974SwiftTestExample[632:11633]Thridtaskisdone
当信号量为3的时候

copy

    19:42:49.912SwiftTestExample[666:12259]Firsttaskstarting
  1. 19:42:49.912SwiftTestExample[666:12258]Secondtaskstarting
  2. 19:42:49.912SwiftTestExample[666:12260]Thridtaskstarting
  3. 19:42:50.915SwiftTestExample[666:12259]Firsttaskisdone
  4. 19:42:50.915SwiftTestExample[666:12260]Thridtaskisdone
  5. 19:42:50.915SwiftTestExample[666:12258]Secondtaskisdone

当信号量为1的时候

copy

    19:43:35.140SwiftTestExample[694:12768]Firsttaskstarting
  1. 19:43:36.145SwiftTestExample[694:12768]Firsttaskisdone
  2. 19:43:36.145SwiftTestExample[694:12771]Secondtaskstarting
  3. 19:43:37.146SwiftTestExample[694:12771]Secondtaskisdone
  4. 19:43:37.146SwiftTestExample[694:12769]Thridtaskstarting
  5. 19:43:38.150SwiftTestExample[694:12769]Thridtaskisdone

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...