Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - Nguyễn Văn Dũng

20
Multi-threading and GCD (grand central dispatch) in iOS Νγυψễν ςăν Δũνγ, Διϖ3 25/5/2016

Transcript of Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - Nguyễn Văn Dũng

Multi-threading and GCD (grand central dispatch) in iOS

Νγυψễν ςăν Δũνγ, Διϖ3

25/5/2016

Threads

•  Threads are a relatively lightweight way to implement multiple paths of execution inside an application •  Threads let you perform more than one action at the same time, and they

execute independently of one another. •  A thread is a combination of the kernel-level and application-level data

structures needed to manage the execution of code •  Has an ID, a stack, thread local storage, priority, CPU registers. •  Share memory and resource within a process.

Multiple threads

• Multiple threads can improve an application’s perceived responsiveness.

• Multiple threads can improve an application’s real-time performance on multicore systems.

•  Queues: •  In iOS multithreading is mostly about “queues”

•  Blocks are lined up in a queue (methods calls can also enqueued)

•  Then those blocks are pulled off the queue and executed on an associated thread

Issues  with  threading    •  Race conditions

• The result depends on the timing of the scheduler

• The behavior is non-deterministic

• Crash

• Wrong result

• Corrupted data

•  When you try to avoid race condition issues, you will use locks/mutexes … => More issues • Dead Locks

• Starvation

• Priority Inversion

Race  conditions  

Demo  

Mutual  Exclusion  

Thread  safely  

•  Thread safe code can be safely called from multiple threads or concurrency tasks without any problem (data corruption, crashing, …)

•  A good approach is to use a concurrent dispatch_queue as read/write lock to maximize performance and try to only lock the areas that are really necessary

Grand  Central  Dispatch  (GCD)  

• What is GCD about?

•  Dispatch block

•  GCD Queues •  Scheduling tasks with queue

•  Synchronizing tasks

•  Serialize async tasks

What  is  GCD  about?  

•  Introduced in OSX (10.6) and iOS (4.0) to make it easier for developers to take advantag of the increasing numbers of CPU cores in devices.

• With GCD you do not interact with thread directly anymore. Instead you add block of code to queues.

•  GCD managers threads pool

• With GCD you will think about work items in a queue rather than threads.

•  Is technology that you use to manage the execution of tasks in your app

•  Is one of the most way to add threads.

Dispatch  block  

•  No arguments

•  No return value

•  Reply on capturing variables

•  ^{…}

GCD Queues

•  FIFO

•  Atomic enqueue

•  Automatic dequeue

GCD Queues

GCD Queues (Serial Queues)  

•  Tasks in this queue execute one at time with FIFO order. Each task starting only after preceding task has finished.

•  Do not know the amount of time between one block ending and next one beginning. The time managed GCD

•  This queue can be use to to protect shared resource to avoid race conditions issues

•  dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);

GCD Queues (Concurrency  Queues)  •  Tasks in this queue still start in order FIFO. But do not wait preceding task has finished.

•  Do not know: •  The order task can finish

•  The time it will take for the next block to start

•  Number of block that are running at any given time.

•  dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT)

GCD  –  Scheduling  Tasks  with  Queues  

•  Task •  dispatch_block_t task1 = ^{};

•  Schedule a task in GCD •  dispatch_sync(queue, task1)

•  dispatch_async(queue, task1)

•  Dispatch_apply(num_runs, queue, ^(size_t t) {})

GCD-­‐  synchronizing  tasks  (dispatch  group)  

•  dispatch_group_create() : create a new empty group

•  dispatch_group_enter(): increments the number of task in group

•  dispatch_group_leave(): decrements the number of task in group

•  Dispatch_group_notify(groupQueue, queue, block ): When there are no tasks left associated with this group.

Demo  

Resources  

•  https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/AboutThreads/AboutThreads.html#//apple_ref/doc/uid/10000057i-­‐CH6-­‐SW3  

•  https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html  

•  https://www.objc.io/issues/2-­‐concurrency/concurrency-­‐apis-­‐and-­‐pitfalls/  

•  Example:  https://github.com/nguyenvandungb/GCDExample