Except for the last article NSThread Threads , One more GCD(Grand Central Dispath), yes Apple A newly developed solution for multi-core programming , Make full use of CPU resources , Put all the tasks , Put it on a task queue for scheduling . GCD The global concurrent queue is provided by default , For the entire application , No need to create .

  • The difference between synchronous and asynchronous

    • dispatch_sync Sync : Does not have the ability to open threads
    • dispatch_async asynchronous : Have the ability to open threads
  • How the task is performed

    • Concurrent queues : Multiple tasks at the same time , Concurrency can only be performed in one step function “ asynchronous “
    • Serial queue : Let the tasks run one by one

Be careful : In general, the most commonly used in actual development is dispatch_async .

Here are some common queue uses

One : The most common global concurrency queue :

 // Get global concurrent queue 
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, )
// Add tasks to the queue Concurrent execution
dispatch_async(queue) { () -> Void in
//doSomething... Mission 1
dispatch_async(queue) { () -> Void in
//doSomething... Mission 2
dispatch_async(queue) { () -> Void in
//doSomething... Mission 3
} // Pause a queue
// Continue queue

Two : Serial queue , Tasks in the queue are not executed concurrently , One task will be executed before another

 // Create a serial queue 
let serialQueue = dispatch_queue_create("dispatch_queue_name", nil)
// Add tasks to the queue After one task is completed, another task is executed
dispatch_async(serialQueue) { () -> Void in
//doSomething... Mission 1
dispatch_async(serialQueue) { () -> Void in
//doSomething... Mission 2
dispatch_async(serialQueue) { () -> Void in
//doSomething... Mission 3

3、 ... and :GCD Communication between threads , It is usually used when the child thread requests the data , Back to the main thread to update UI

 // Inter thread communication 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, )) { () -> Void in
// Do something in the subthread
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Update in main thread UI...

Four : Thread delay execution

 // Thread delay execution 
let second:Double = 3.0
let dtime = dispatch_time(DISPATCH_TIME_NOW, Int64(second * Double(NSEC_PER_SEC))) //3s Then go back to the main thread to execute the task
dispatch_after(dtime, dispatch_get_main_queue()) { () -> Void in }
//3s After that, a new thread will be started to execute the task in the queue
dispatch_after(dtime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, )) { () -> Void in }

5、 ... and : One time code , seeing the name of a thing one thinks of its function , In the application life cycle ,block The code block is executed only once .

 var once:dispatch_once_t =
dispatch_once(&once, { () -> Void in
// Only once in the application life cycle

6、 ... and : Queue group : Put some tasks into a queue group , When all tasks are completed , I'll run the team notify.

 /********************************** Queue group ******************************************/
let group = dispatch_group_create()
let groupQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, )
dispatch_group_async(group, groupQueue){ () -> Void in
// Mission 1
dispatch_group_async(group, groupQueue){ () -> Void in
// Mission 2
dispatch_group_async(group, groupQueue){ () -> Void in
// Mission 3
dispatch_group_notify(group, groupQueue) { () -> Void in
// I'll wait for the mission 1、 Mission 2、 Mission 3 Execute after execution

End !

iOS Development :Swift Multithreading GCD More articles about the use of

  1. ios Multithreading of development ---GCD

    One : Basic concepts 1: process : The running program is a process . 2: Threads : Each process must have threads to perform tasks , In process tasks are executed in threads . 3: Thread serialization : The execution of tasks in a thread is serial , Suppose a process opens up a thread ...

  2. iOS Development - Swift Use GCD Realize the timer function

    Preface In development , Regular execution of network requests is often used . count down . Timer and other functions , This article is introduced in iOS In development ,Swift How to use GCD Implement these functions . Do it once The following code will be in 5 Seconds later , And only once . let tim ...

  3. IOS Multithreading of development -- GCD In every way

    Preface : This article GCD I have read a lot about the gods at home and abroad GCD The article , As well as the combination of their own before GCD A superficial understanding of , Then extract its essence , Get rid of it , Combined notes , And as far as possible in a familiar and easy to understand and correct way of theoretical exposition ...

  4. iOS Development - Multithreading programming technology (Thread、Cocoa operations、GCD)

    brief introduction In software development , Multithreading programming technology is widely used , I believe multithreading is no longer strange to us . With multithreading technology , We can do many things together , Instead of doing it one task at a time . such as : The front end interacts with the back end . Big task ( It takes time ...

  5. iOS Multithreading of development (NSThread、NSOperation、GCD)

    Sort out some knowledge about multithreading . parallel & Concurrent 1. parallel : Parallelism is relative to multicore , Several tasks are performed at the same time .2. Concurrent : Concurrency is relative to a single core , Switch between several tasks quickly , Look like " meanwhile " ...

  6. iOS Development of multithreading technology

    This article strives for a clear and thorough explanation , Will still be clear through the four aspects of iOS The usage of multithreading in development is explained in detail : One . What is multithreading 1) The principle of multithreading execution 2) Threads and processes 3) The advantages and disadvantages of multithreading Two . Why do we use multithreading 3、 ... and ...

  7. iOS Development Swift piece (02) NSThread Thread related simple description

    iOS Development Swift piece (02) NSThread Thread related simple description One explain 1) About multithreading part of the theoretical knowledge and OC Realization , It has been written in the previous blog , So I'm not going to say . 2) This article simply explains NSThread stay s ...

  8. Li Hongqiang iOS Development Swift piece ---12_NSThread Thread related simple description

    Li Hongqiang iOS Development Swift piece ---12_NSThread Thread related simple description One explain 1) About multithreading part of the theoretical knowledge and OC Realization , It has been written in the previous blog , So I'm not going to say . 2) This article simply explains NSThre ...

  9. iOS Development - Thread safety of multithreading development

    Preface : A resource may be shared by multiple threads , That is, multiple threads may access the same block of resources , For example, multiple threads access the same object . The same variable . The same file and the same method, etc . So when multiple threads access the same resource , It's easy to have data errors and data insecurity ...

  10. iOS Development Swift piece —( One ) Brief introduction

    iOS Development Swift piece — Brief introduction One . brief introduction Swift It 's apple on 2014 year WWDC( Apple developer conference ) New programming language released Swift In the Chinese dynasty “ Swifts ”, It is the LOGO It's a swallow , Follow Objective-C ...

Random recommendation

  1. Linux Command learning summary :cd command

    Command profile : This command is used to switch the current directory .cd yes change directory Abbreviation Command syntax : cd [-L|-P] [dir] Examples of use 1: Switch to a directory higher than the current directory 1: [root@DB-S ...

  2. I2C Controller Verilog Modeling two

    Preface : And then the last one I2C Write operations , Today we want to achieve a I2C Read operation . Although in ADV7181B There is no need to use read operations when configuring internal registers , But to further confirm if the register is in I2C The write module is configured correctly , This step is essential . ...

  3. Android PullToRefresh (ListView GridView The drop-down refresh ) The use of, ( Reprint )

    Recently, the project uses the drop-down refresh , Load more , Here to PullToRefresh This control to understand and use . The following is reproduced from :http://blog.csdn.net/lmj623565791/article/deta ...

  4. The suffix array ---Milk Patterns

    POJ  3261 Description Farmer John has noticed that the quality of milk given by his cows varies from ...

  5. In depth understanding of HBase Memstore

    2013/08/09 Forward from http://www.cnblogs.com/shitouer/archive/2013/02/05/configuring-hbase-memstore-what-you ...

  6. Xlint as well as Java Lint Options

    Java Lint Options Java Compiler options include so-called standard options and non-standard options . The standard option is to support... In the current version of the development environment , Options that will be supported in future releases . Common standard options like  -classpath as well as ...

  7. HDU 1988 Cube Stacking ( data structure - And check the set )

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18834   Accepted: 6535 Ca ...

  8. GET and POST The difference between , When to use POST?

     GET: Generally used to obtain information , Use URL Pass parameters , There are also restrictions on the number of messages sent , Generally in 2000 Characters     POST: Generally used to modify resources on the server , There are no restrictions on the information sent .     GET Method needs to be used Re ...

  9. php Debugging tools for xdebug

    zend_extension = "D:/developsoftware/wamp/bin/php/php5.5.12/zend_ext/php_xdebug-2.2.5-5.5-vc11- ...

  10. Java Multithreading concurrent tool class

    Semaphore- Signal mechanism When we create a scalable thread pool , And a limited number of threads need to run concurrently in the thread pool , It needs to be used Semaphore( Signal mechanism ),Semaphore Usually used to restrict access to ...