Your SlideShare is downloading. ×
0
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Presentation 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Presentation 2

76

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
76
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. [iOS  multithreading:GCD];
  • 2. •Multithreading in iOS •Grand Central Dispatch •Queues •Example
  • 3. Multithreading in iOS
  • 4. •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
  • 5. Pthreads NSThreads GCD NSOperation complexity low high abstraction level low high
  • 6. Grand Central Dispatch
  • 7. 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
  • 8. • define the tasks • block or function • add them to an appropriate dispatch queue dispatch_async(queue, ^{ some_async_work(); });
  • 9. thread thread thread Queue based task task task task task queue task task task task task queue task task task
  • 10. 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
  • 11. Frequently Used APIs • dispatch_async(queue, block) • dispatch_once(queue, block) • dispatch_apply(iterations, queue, block) • dispatch_group_async(group, queue, block)
  • 12. Example
  • 13. 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
  • 14. 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; } } } });
  • 15. 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; } } } });
  • 16. Results • iPhone 5 : dual-core A6 chip • dictionary : 25000 words 1.86 times faster Sequential GCD 274s 147s

×