Original blog, reproduced please indicate the source
Http://blog.csdn.net/hello_hwc?viewmode=contents
What is the semaphore?
The simple thing is to control the number of access resources, such as the system has two resources can be exploited, while there are three threads to access, can only allow two threads access, the third should wait for resources to be released before access.
Note: In the GCD, only the scheduled thread will enter the kernel state for thread blocking when the semaphore is insufficient.
Second how to use the semaphore
Three main functions
Create a semaphore
Func Dispatch_semaphore_create (_ Value:int), dispatch_semaphore_t!
Where value is the initial value of the semaphore and returns null if it is less than 0
Increase signal Volume
Func dispatch_semaphore_signal (_ dsema:dispatch_semaphore_t!), Int
Wait for lower signal volume
Func dispatch_semaphore_wait (_ dsema:dispatch_semaphore_t!, _ timeout:dispatch_time_t), Int
Note that the normal order of use is first lowered and then raised, and these two functions are usually used in pairs.
Three examples of analysis
<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:ade Coder)} override func Viewdidload () {super.viewdidload () Dispatch_async (Dispatch_get_global_queue (DI spatch_queue_priority_default,0), {(), Void in Self.task_first ()}) Dispatch_async (DISPATC H_get_global_queue (dispatch_queue_priority_default,0), {(), Void in Self.task_second ()}) d Ispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default,0), {(), Void in Self.task_third ( )})/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_sec Ond () {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 F UNC didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be RECR Eated. }}
This code simulates committing three tasks, committing to a global queue (parallel queue)
When the initial start of the semaphore is 2.
Output
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
When the signal volume is 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 was done
When the signal volume is 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
Full details GCD series (quad) Dispatch_semaphore (semaphore)