完整詳解GCD系列(四)dispatch_semaphore(訊號量),dispatchsemaphore

來源:互聯網
上載者:User

完整詳解GCD系列(四)dispatch_semaphore(訊號量),dispatchsemaphore

原創Blog,轉載請註明出處

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


一 何為訊號量?

  簡單來說就是控制訪問資源的數量,比如系統有兩個資源可以被利用,同時有三個線程要訪問,只能允許兩個線程訪問,第三個應當等待資源被釋放後再訪問。

注意:再GCD中,只有調度的線程在訊號量不足的時候才會進入核心態進行線程阻塞

二 如何使用訊號量

三個主要函數

建立一個訊號量

func dispatch_semaphore_create(_ value: Int) ->  dispatch_semaphore_t !
其中value為訊號量的初值,如果小於0則會返回NULL


提高訊號量

func dispatch_semaphore_signal(_ dsema: dispatch_semaphore_t!) -> Int

等待降低訊號量

func dispatch_semaphore_wait(_ dsema: dispatch_semaphore_t!,                           _ timeout: dispatch_time_t) -> Int
注意,正常的使用順序是先降低然後再提高,這兩個函數通常成對使用。

三 舉例分析

<pre name="code" class="objc">////  ViewController.swift//  SwiftTestExample////  Created by huangwenchen on 15/1/6.//  Copyright (c) 2015年 huangwenchen. All rights reserved.//import UIKitclass ViewController: UIViewController {    var semaphore:dispatch_semaphore_t;    required init(coder aDecoder: NSCoder) {        self.semaphore = dispatch_semaphore_create(1)        super.init(coder: aDecoder)    }    override func viewDidLoad() {        super.viewDidLoad()        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {() -> Void  in             self.task_first()        })        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in            self.task_second()        })        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in            self.task_third()        })        // Do any additional setup after loading the view, typically from a nib.    }    func task_first(){        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)        NSLog("%@","First task starting")        sleep(1)        NSLog("%@", "First task is done")        dispatch_semaphore_signal(self.semaphore)    }    func task_second(){        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)        NSLog("%@","Second task starting")        sleep(1)        NSLog("%@", "Second task is done")        dispatch_semaphore_signal(self.semaphore)    }    func task_third(){        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)        NSLog("%@","Thrid task starting")        sleep(1)        NSLog("%@", "Thrid task is done")        dispatch_semaphore_signal(self.semaphore)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

這段代碼類比提交三個任務,提交到全域隊列(並行隊列)

當訊號量的初初始為2時候

輸出

2015-01-06 19:42:01.963 SwiftTestExample[632:11631] First task starting2015-01-06 19:42:01.964 SwiftTestExample[632:11630] Second task starting2015-01-06 19:42:02.971 SwiftTestExample[632:11630] Second task is done2015-01-06 19:42:02.971 SwiftTestExample[632:11631] First task is done2015-01-06 19:42:02.971 SwiftTestExample[632:11633] Thrid task starting2015-01-06 19:42:03.974 SwiftTestExample[632:11633] Thrid task is done
當訊號量為3的時候

2015-01-06 19:42:49.912 SwiftTestExample[666:12259] First task starting2015-01-06 19:42:49.912 SwiftTestExample[666:12258] Second task starting2015-01-06 19:42:49.912 SwiftTestExample[666:12260] Thrid task starting2015-01-06 19:42:50.915 SwiftTestExample[666:12259] First task is done2015-01-06 19:42:50.915 SwiftTestExample[666:12260] Thrid task is done2015-01-06 19:42:50.915 SwiftTestExample[666:12258] Second task is done

當訊號量為1的時候

2015-01-06 19:43:35.140 SwiftTestExample[694:12768] First task starting2015-01-06 19:43:36.145 SwiftTestExample[694:12768] First task is done2015-01-06 19:43:36.145 SwiftTestExample[694:12771] Second task starting2015-01-06 19:43:37.146 SwiftTestExample[694:12771] Second task is done2015-01-06 19:43:37.146 SwiftTestExample[694:12769] Thrid task starting2015-01-06 19:43:38.150 SwiftTestExample[694:12769] Thrid task is done


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.