2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone...
-
Upload
coleen-riley -
Category
Documents
-
view
217 -
download
0
description
Transcript of 2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone...
05/05/23EEC492/693/793 - iPhone Application
Development 105/05/23EEC492/693/793 - iPhone Application
Development 1
EEC-492/693/793EEC-492/693/793iPhone Application iPhone Application DevelopmentDevelopment
Lecture 22Lecture 22Concurrency and Multithreading Concurrency and Multithreading (slides based on CS193P, Stanford)(slides based on CS193P, Stanford)
Wenbing Zhao & Nigamanth SridharWenbing Zhao & Nigamanth Sridhar
Concurrency
With only one thread, long-running operations can interfere with user interaction System can’t update the screen App looks like it’s hung
Multiple threads allow multiple computations without locking up your entire application Blocking operation can happen on the
background thread
05/05/23EEC492/693/793 - iPhone Application
Development 2
Threads on iPhone
Based on POSIX threads API /usr/include/pthread.h
Higher-level wrappers in Foundation framework NSThread Object-oriented, easy to use
05/05/23EEC492/693/793 - iPhone Application
Development 3
NSThread
Run loop automatically instantiated for each thread Run loop is the main event loop that the app runs App spends most of its time in the run loop
Each NSThread needs to create its own autorelease pool One is not created for you
Includes methods for inter-thread messaging
05/05/23EEC492/693/793 - iPhone Application
Development 4
NSThread
Creating a new thread Attach a selector to the new thread
The selector method does the actual work Once the work is complete, the selector on
the background thread sends a message back to the main thread to notify completion All interaction with the user must happen on the
main thread
05/05/23EEC492/693/793 - iPhone Application
Development 5
Using NSThread
05/05/23EEC492/693/793 - iPhone Application
Development 6
- (void)someAction:(id)sender { // Fire up new thread [NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData];}
- (void)doWork:(id)someData { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [someData doLotsOfWork];
// Send message back to main thread [self performSelectorOnMainThread:@selector(allDone:) withObject:[someData result] waitUntilDone:NO]; [pool release];}
Dangers of Multiple Threads
Can add considerable amount of complexity to app design
Threads share memory; they have access to the same data structures
Need coordination to decide which thread has access to a particular data structure at a particular time Synchronization and Locking
05/05/23EEC492/693/793 - iPhone Application
Development 7
UIKit and Threadsafety Threadsafety
A single data object accessed in two different threads may lead to a race condition
UIKit classes are not threadsafe Objects must be created and messaged from the
main thread Notable exception: UIImage can be created in a
background thread But since UIImageView is not threadsafe, you can’t
set it in a background thread
05/05/23EEC492/693/793 - iPhone Application
Development 8
Locks
Protect critical sections of code Thread-safety NSLock
05/05/23EEC492/693/793 - iPhone Application
Development 9
- (void)init { myLock = [[NSLock alloc] init];}- (void)someMethod { [myLock lock]; // Execute critical section code [myLock unlock];}
Conditions
NSCondition is useful for producer/consumer model
05/05/23EEC492/693/793 - iPhone Application
Development 10
// On the producer thread- (void) produceData { [condition lock]; // Produce new data newDataExists = YES; [condition signal]; [condition unlock];}
// On the consumer thread- (void) consumeData { [condition lock]; while(!newDataExists) { [condition wait]; } // Consume the new data newDataExists = NO; [condition unlock];}
Danger of Locks
Very easy to make mistakes All it takes is one “poorly behaved” client
Accessing shared data outside of a lock Deadlocks Priority inversion
Main thread is always supposed to have highest priority
05/05/23EEC492/693/793 - iPhone Application
Development 11
Threading Pitfalls
Subtle, nondeterministic bugs may be introduced Notoriously difficult to replicate
Code may become more difficult to maintain Poorly managed threads and synchronization
may actually result in slower code
05/05/23EEC492/693/793 - iPhone Application
Development 12
Alternatives to Explicit Threading Asynchronous (nonblocking) functions
Call a function, and pass a pointer to a callback function
Specify a target/action or a delegate for callback Timers
One-shot or recurring Specify a callback method Managed by the run loop
NSOperation: higher level construct
05/05/23EEC492/693/793 - iPhone Application
Development 13
NSOperation
Manages thread creation and lifecycle Encapsulate a unit of work in an object
System can schedule the operation when it is appropriate
Specify priorities and dependencies
05/05/23EEC492/693/793 - iPhone Application
Development 14
Creating an NSOperation Subclass Define a custom init method
Override main method
05/05/23EEC492/693/793 - iPhone Application
Development 15
- (id)initWithSomeObject:(id)someObject { self = [super init]; if (self) { self.someObject = someObject; } return self;}
- (void)main { [someObject doLotsOfTimeConsumingWork];}
NSOperationQueue
Operations are typically scheduled by adding to a queue
Choose a maximum number of concurrent operations Runtime worries about creating an appropriate
number of threads Queue runs operations based on priority and
dependencies
05/05/23EEC492/693/793 - iPhone Application
Development 16
NSInvocationOperation
Concrete subclass of NSOperation For lightweight tasks
05/05/23EEC492/693/793 - iPhone Application
Development 17
- (void)someAction:(id)sender { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doWork:) object:someObject]; [queue addOperation:operation]; [operation release];}