Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Parallele Programmierung (DE)
Search
Chris Eidhof | @chriseidhof
September 11, 2013
Technology
170
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Parallele Programmierung (DE)
Bei CocoaHeads Dresden
Chris Eidhof | @chriseidhof
September 11, 2013
More Decks by Chris Eidhof | @chriseidhof
See All by Chris Eidhof | @chriseidhof
Dutch FP Day 2015
chriseidhof
2
410
Tiny Networking in Swift
chriseidhof
2
19k
Functional Swift - Brooklyn
chriseidhof
3
1.3k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.3k
Functional Swift
chriseidhof
1
180
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
220
Parsing with Blocks
chriseidhof
2
270
Other Decks in Technology
See All in Technology
Issue 駆動でスペシャリストの意図を届ける、AI 実装のアクセシビリティ向上
thkt
0
120
Cloudflare Workers 向けアプリを C# で構築する ~WASM Native AOT への道~
nenonaninu
1
300
AIエージェントの一手は 誰も見ていない - Falco拡張OSS「Prempti」とeBPFで サーバーレス実行基盤を二層防御する
keitah
0
200
事業課題から技術的負債に向き合う
sansantech
PRO
2
2k
バイブコーディング時代のWebアプリ開発入門~Cloud Runで学ぶセキュアなビルドとデプロイ
waiwai2111
1
140
AIを活用するために決めた "やらないこと" - 価値に注目する / Not betting on AI
soudai
PRO
2
550
AI 時代のスタートアップエコシステ厶から考究する技術的負債との向き合い方
m3m0r7
PRO
3
2.3k
SREは、MCPとAutopilotをこう使え!
kazumax55
3
890
リアーキテクチャ後の障害ゼロを目指したShadow Testingの取り組み
nihonbuson
PRO
1
160
EventBridge に「合流」はない ― サーバーレスのワークフローを育てるということ / No Join in EventBridge
yusukeshimizu
2
140
登壇の自信を奪う3匹のオバケ / 3 Ghosts That Rob You of Your Confidence in Public Speaking
pauli
9
1k
SREへの勘違いに気づいた後の話
tomodakengo
0
110
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
410
Evolving SEO for Evolving Search Engines
ryanjones
0
300
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
410
How STYLIGHT went responsive
nonsquared
100
6.3k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
760
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
250
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
460
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
510
We Have a Design System, Now What?
morganepeng
55
8.3k
Abbi's Birthday
coloredviolet
4
10k
Scaling GitHub
holman
464
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Transcript
Parallele Programmierung Chris Eidhof Sunday, September 22, 13
Achtung Sunday, September 22, 13
Nicht alles muss async sein • Schwer zu debuggen •
Schwer zu lesen und schreiben • Fehleranfällig Sunday, September 22, 13
APIs • OpenCL • pthread • NSRunLoop • NSThread •
Grand Central Dispatch • Operation Queues Sunday, September 22, 13
pthread size_t count; }; struct threadResult { uint32_t min; uint32_t
max; }; void * findMinAndMax(void *arg) { struct threadInfo const * const info = (struct threadInfo *) arg; uint32_t min = UINT32_MAX; uint32_t max = 0; for (size_t i = 0; i < info->count; ++i) { uint32_t v = info->inputValues[i]; min = MIN(min, v); max = MAX(max, v); } free(arg); struct threadResult * const result = (struct threadResult *) malloc(sizeof(*result)); result->min = min; result->max = max; return result; } int main(int argc, const char * argv[]) { size_t const count = 1000000; uint32_t inputValues[count]; // Fill input values with random numbers: for (size_t i = 0; i < count; ++i) { inputValues[i] = arc4random(); } // Spawn 4 threads to find the minimum and maximum: size_t const threadCount = 4; pthread_t tid[threadCount]; for (size_t i = 0; i < threadCount; ++i) { struct threadInfo * const info = (struct threadInfo *) malloc(sizeof(*info)); size_t offset = (count / threadCount) * i; info->inputValues = inputValues + offset; info->count = MIN(count - offset, count / threadCount); int err = pthread_create(tid + i, NULL, &findMinAndMax, info); NSCAssert(err == 0, @"pthread_create() failed: %d", err); } // Wait for the threads to exit: struct threadResult * results[threadCount]; for (size_t i = 0; i < threadCount; ++i) { int err = pthread_join(tid[i], (void **) &(results[i])); NSCAssert(err == 0, @"pthread_join() failed: %d", err); } // Find the min and max: uint32_t min = UINT32_MAX; uint32_t max = 0; for (size_t i = 0; i < threadCount; ++i) { Sunday, September 22, 13
NSRunloop [self performSelector:@selector(mySelector:) withObject:nil afterDelay:1.0]; Sunday, September 22, 13
@interface FindMinMaxThread : NSThread @property (nonatomic) NSUInteger min; @property (nonatomic)
NSUInteger max; - (instancetype)initWithNumbers:(NSArray *)numbers; @end @implementation FindMinMaxThread { NSArray *_numbers; } - (instancetype)initWithNumbers:(NSArray *)numbers { self = [super init]; if (self) { _numbers = numbers; } return self; } - (void)main { NSUInteger min; NSUInteger max; // process the data self.min = min; self.max = max; } @end NSSet *threads = [NSMutableSet set]; NSUInteger numberCount = self.numbers.count; NSUInteger threadCount = 4; for (NSUInteger i = 0; i < threadCount; i++) { NSUInteger offset = (count / threadCount) * i; NSUInteger count = MIN(numberCount - offset, numberCount / threadCount); NSRange range = NSMakeRange(offset, count); NSArray *subset = [self.numbers subarrayWithRange:range]; FindMinMaxThread *thread = [[FindMinMaxThread alloc] initWithNumbers:subset]; [threads addObject:thread]; [thread start]; } NSThread Sunday, September 22, 13
Wie viele Threads brauchen wir? Sunday, September 22, 13
Grand Central Dispatch Sunday, September 22, 13
Sunday, September 22, 13
Sunday, September 22, 13
Sunday, September 22, 13
Parallel Seriell Sunday, September 22, 13
Prioritätsinversion R N H M Sunday, September 22, 13
Async Blocks dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(defaultQueue, ^{ [self
expensiveBackgroundCode]; }); Sunday, September 22, 13
UI Aktualisieren dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(defaultQueue, ^{ [self
expensiveBackgroundCode]; dispatch_async(dispatch_get_main_queue(), ^{ self.label.text = @"Done"; }); }); Sunday, September 22, 13
Mein Code: dispatch_async(backgroundQueue, ^{ NSData* contents = [NSData dataWithContentsOfURL:url]; dispatch_async(dispatch_get_main_queue(),
^{ // do something with the data. }); }); Sunday, September 22, 13
Probleme • Nicht abbrechbar • Blockierend Sunday, September 22, 13
NSOperationQueue NSOperationQueue* backgroundQueue; [backgroundQueue addOperation:downloadOperation]; Sunday, September 22, 13
Abbrechen [downloadOperation cancel]; Sunday, September 22, 13
Blocks [backgroundQueue addOperationWithBlock:^{ [self expensiveOperation]; }]; Sunday, September 22, 13
Eigene Operations @interface DownloadOperation : NSOperation @end Sunday, September 22,
13
Weitere Aspekte • Abhängigkeiten • maxConcurrentOperationCount • Auf Grand Central
Dispatch basiert Sunday, September 22, 13
Unterklassen • NSInvocationOperation • NSBlockOperation Sunday, September 22, 13
Async Core Data Am einfachsten: über den Haupt-Thread Sunday, September
22, 13
NSManagedObjectContext* context = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType]; context.persistentStoreCoordinator = self.persistentStoreCoordinator;
Sunday, September 22, 13
[context performBlock:^{ [self expensiveImportOperation]; }]; Sunday, September 22, 13
Main context aktualisieren [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification object:nil queue:nil usingBlock:^(NSNotification* note)
{ NSManagedObjectContext *moc = mainManagedObjectContext; if (note.object != moc) { [moc performBlock:^(){ [moc mergeChangesFromContextDidSaveNotification:note]; }]; } }]; Sunday, September 22, 13
Es gibt noch immer ein Lock Du kannst aber zwei
Persistent Store Coordinators verwenden Sunday, September 22, 13
UI im Hintergrund [operationQueue addOperationWithBlock:^{ NSNumber* result = findLargestMersennePrime(); [[NSOperationQueue
mainQueue] addOperationWithBlock:^{ self.textLabel.text = [result stringValue]; }]; }]; Sunday, September 22, 13
UIKit ist nicht Threadsicher Sunday, September 22, 13
Parallel zeichnen UIGraphicsBeginImageContextWithOptions(size, NO, 0); // drawing code here UIImage
*i = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return i; Sunday, September 22, 13
Parallel zeichnen • Nur wenn es notwendig ist • Vielleicht
kannst du es vermeiden • WWDC 2012 - Building Concurrent User Interfaces on iOS Sunday, September 22, 13
More reading • http://www.objc.io/issue-2 • Concurrency Programming Guide Sunday, September
22, 13
Kontakt @chriseidhof http://www.objc.io http://www.uikonf.com http://chris.eidhof.nl Sunday, September 22, 13