當前位置:
首頁 > 最新 > 完整詳解GCD系列(四)dispatch

完整詳解GCD系列(四)dispatch

////-------------注意----------------

這篇文章是在Swift 1.0時代寫的,已經不適合當前的語法。關於Swift最新版本的GCD,參見我的這篇博客

GCD精講

/////-------------注意----------------

一 何為信號量?

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

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

二 如何使用信號量

三個主要函數

創建一個信號量

[plain]view plaincopy

func dispatch_semaphore_create(_ value: Int) -> dispatch_semaphore_t !

其中value為信號量的初值,如果小於0則會返回NULL

提高信號量

[plain]view plaincopy

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

等待降低信號量

[plain]view plaincopy

func dispatch_semaphore_wait(_ dsema: dispatch_semaphore_t!,

_ timeout: dispatch_time_t) -> Int

注意,正常的使用順序是先降低然後再提高,這兩個函數通常成對使用。

三 舉例分析

[plain]view plaincopy

//

// ViewController.swift

// SwiftTestExample

//

// Created by huangwenchen on 15/1/6.

// Copyright (c) 2015年 huangwenchen. All rights reserved.

//

import UIKit

class 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時候

輸出

[objc]view plaincopy

215-1-619:42:1.963SwiftTestExample[632:11631] First task starting

215-1-619:42:1.964SwiftTestExample[632:1163] Second task starting

215-1-619:42:2.971SwiftTestExample[632:1163] Second task is done

215-1-619:42:2.971SwiftTestExample[632:11631] First task is done

215-1-619:42:2.971SwiftTestExample[632:11633] Thrid task starting

215-1-619:42:3.974SwiftTestExample[632:11633] Thrid task is done

當信號量為3的時候

[objc]view plaincopy

215-1-619:42:49.912SwiftTestExample[666:12259] First task starting

215-1-619:42:49.912SwiftTestExample[666:12258] Second task starting

215-1-619:42:49.912SwiftTestExample[666:1226] Thrid task starting

215-1-619:42:5.915SwiftTestExample[666:12259] First task is done

215-1-619:42:5.915SwiftTestExample[666:1226] Thrid task is done

215-1-619:42:5.915SwiftTestExample[666:12258] Second task is done

當信號量為1的時候

[objc]view plaincopy

215-1-619:43:35.140SwiftTestExample[694:12768] First task starting

215-1-619:43:36.145SwiftTestExample[694:12768] First task is done

215-1-619:43:36.145SwiftTestExample[694:12771] Second task starting

215-1-619:43:37.146SwiftTestExample[694:12771] Second task is done

215-1-619:43:37.146SwiftTestExample[694:12769] Thrid task starting

215-1-619:43:38.150SwiftTestExample[694:12769] Thrid task is done

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Swift技術專欄 的精彩文章:

TAG:Swift技術專欄 |