Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application.

Similar presentations


Presentation on theme: "2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application."— Presentation transcript:

1 2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application Development Lecture 22 Concurrency and Multithreading (slides based on CS193P, Stanford) Wenbing Zhao & Nigamanth Sridhar

2 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 2/20/2016 EEC492/693/793 - iPhone Application Development 2

3 Threads on iPhone Based on POSIX threads API  /usr/include/pthread.h Higher-level wrappers in Foundation framework  NSThread  Object-oriented, easy to use 2/20/2016 EEC492/693/793 - iPhone Application Development 3

4 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 2/20/2016 EEC492/693/793 - iPhone Application Development 4

5 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 2/20/2016 EEC492/693/793 - iPhone Application Development 5

6 Using NSThread 2/20/2016 EEC492/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]; }

7 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 2/20/2016 EEC492/693/793 - iPhone Application Development 7

8 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 2/20/2016 EEC492/693/793 - iPhone Application Development 8

9 Locks Protect critical sections of code Thread-safety NSLock 2/20/2016 EEC492/693/793 - iPhone Application Development 9 - (void)init { myLock = [[NSLock alloc] init]; } - (void)someMethod { [myLock lock]; // Execute critical section code [myLock unlock]; }

10 Conditions NSCondition is useful for producer/consumer model 2/20/2016 EEC492/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]; }

11 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 2/20/2016 EEC492/693/793 - iPhone Application Development 11

12 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 2/20/2016 EEC492/693/793 - iPhone Application Development 12

13 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 2/20/2016 EEC492/693/793 - iPhone Application Development 13

14 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 2/20/2016 EEC492/693/793 - iPhone Application Development 14

15 Creating an NSOperation Subclass Define a custom init method Override main method 2/20/2016 EEC492/693/793 - iPhone Application Development 15 - (id)initWithSomeObject:(id)someObject { self = [super init]; if (self) { self.someObject = someObject; } return self; } - (void)main { [someObject doLotsOfTimeConsumingWork]; }

16 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 2/20/2016 EEC492/693/793 - iPhone Application Development 16

17 NSInvocationOperation Concrete subclass of NSOperation For lightweight tasks 2/20/2016 EEC492/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]; }


Download ppt "2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application."

Similar presentations


Ads by Google