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

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

description

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

Transcript of 2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone...

Page 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.

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

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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];}

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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];}

Page 10: 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.

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];}

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

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];}

Page 16: 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.

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

Page 17: 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.

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];}