Slide 1

Slide 1 text

[iOS  multithreading:GCD];

Slide 2

Slide 2 text

• Multithreading in iOS • Grand Central Dispatch • Queues • Example

Slide 3

Slide 3 text

Multithreading in iOS

Slide 4

Slide 4 text

• Pthreads : C-based interface for creating and manipulating threads • NSThread : Cocoa threads • Grand Central Dispatch : a lightweight multithreading engine developed by Apple Inc • NSOperation : wrapper for task

Slide 5

Slide 5 text

Pthreads NSThreads GCD NSOperation complexity low high abstraction level low high

Slide 6

Slide 6 text

Grand Central Dispatch

Slide 7

Slide 7 text

What is GCD? • A lightweight multithreading engine • Uses a thread pool • Automatically optimizes threading • Scheduling of tasks • Uses look-free exclusion rather than mutual exclusion

Slide 8

Slide 8 text

• define the tasks • block or function • add them to an appropriate dispatch queue dispatch_async(queue, ^{ some_async_work(); });

Slide 9

Slide 9 text

thread thread thread Queue based task task task task task queue task task task task task queue task task task

Slide 10

Slide 10 text

Queue types • Serial Queues • only one task running at a time • user queue or main queue • Concurrent Queue • tasks started in order but run concurrently • 3 priority levels: HIGHT, DEFAULT, LOW

Slide 11

Slide 11 text

Frequently Used APIs • dispatch_async(queue, block) • dispatch_once(queue, block) • dispatch_apply(iterations, queue, block) • dispatch_group_async(group, queue, block)

Slide 12

Slide 12 text

Example

Slide 13

Slide 13 text

Palindromic words • palindromic words - ճจ • is a word that reads the same forward as it does backward : “radar” • problem • count the palindromic words in a dictionary radar wrong way yaw result = 3

Slide 14

Slide 14 text

for (size_t index = 0; index < Words.count; index++) { NSString *word = [Words objectAtIndex:index]; NSString *reverseString = [self reverseString:word]; if([word caseInsensitiveCompare:reverseString] == NSOrderedSame) { self.palindromicCount++; } else { for (size_t i = index + 1; i < Words.count - 1; i++) { NSString *secondWord = [Words objectAtIndex:i]; if([reverseString caseInsensitiveCompare:secondWord] == NSOrderedSame) { self.palindromicCount += 2; break; } } } }; dispatch_apply(Words.count, queue, ^(size_t index) { NSString *word = [Words objectAtIndex:index]; NSString *reverseString = [self reverseString:word]; if([word caseInsensitiveCompare:reverseString] == NSOrderedSame) { self.palindromicCount++; } else { for (size_t i = index + 1; i < Words.count - 1; i++) { NSString *secondWord = [Words objectAtIndex:i]; if([reverseString caseInsensitiveCompare:secondWord] == NSOrderedSame) { self.palindromicCount += 2; break; } } } });

Slide 15

Slide 15 text

for (size_t index = 0; index < Words.count; index++) { NSString *word = [Words objectAtIndex:index]; NSString *reverseString = [self reverseString:word]; if([word caseInsensitiveCompare:reverseString] == NSOrderedSame) { self.palindromicCount++; } else { for (size_t i = index + 1; i < Words.count - 1; i++) { NSString *secondWord = [Words objectAtIndex:i]; if([reverseString caseInsensitiveCompare:secondWord] == NSOrderedSame) { self.palindromicCount += 2; break; } } } }; dispatch_apply(Words.count, queue, ^(size_t index) { NSString *word = [Words objectAtIndex:index]; NSString *reverseString = [self reverseString:word]; if([word caseInsensitiveCompare:reverseString] == NSOrderedSame) { self.palindromicCount++; } else { for (size_t i = index + 1; i < Words.count - 1; i++) { NSString *secondWord = [Words objectAtIndex:i]; if([reverseString caseInsensitiveCompare:secondWord] == NSOrderedSame) { self.palindromicCount += 2; break; } } } });

Slide 16

Slide 16 text

Results • iPhone 5 : dual-core A6 chip • dictionary : 25000 words 1.86 times faster Sequential GCD 274s 147s