Aleksandr_Butenko_Mobile_Development

23
Multitasking in iOS Alexander Butenko, Ciklum

Transcript of Aleksandr_Butenko_Mobile_Development

Page 1: Aleksandr_Butenko_Mobile_Development

Multitasking in iOS

Alexander Butenko, Ciklum

Page 2: Aleksandr_Butenko_Mobile_Development

What is a multitasking?§ Multitasking is a method where multiple tasks share

resources such as a CPU. In the case of a computer with a single CPU, only one task is said to be running at any point in time, meaning that the CPU is actively executing instructions for that task. Multitasking solves the problem by scheduling which task may be the one running at any given time, and when another waiting task gets a turn.

Page 3: Aleksandr_Butenko_Mobile_Development

Multitasking goals

§ Improvement of application’s perceived responsiveness.

§ Improvement of application’s real-time performance on multicore systems.

Page 4: Aleksandr_Butenko_Mobile_Development

Basics To run program system need to make environment for

task execution. This environment consists of memory resources, input-output posibillity, access to different system resources including kernel services. This environment is called process.

In contrast to the process, which is equipped to create their own address space and context, thread uses the address space of the parent process and part of its context. So independent threads created within a single process can access the same data.

Page 5: Aleksandr_Butenko_Mobile_Development

Threads advantages

§ Thread need less system resources (time for creation and physical resources) than processes

§ Interthread communication easier than interprocess communication

Page 6: Aleksandr_Butenko_Mobile_Development

Threads disadvantages

§ Thread add uncertainty to your code

§ Threads still introduce a tremendous amount of overhead to your process

Page 7: Aleksandr_Butenko_Mobile_Development

Threads alternatives

§ Operation objects

§ Grand Central Dispatch (GCD)

§ Idle-time notifications

§ Asynchronous functions

§ Timers

Page 8: Aleksandr_Butenko_Mobile_Development

Threads in iOS

§ Сocoa threads

§ POSIX threads

Page 9: Aleksandr_Butenko_Mobile_Development

Сocoa threads

Cocoa implements threads using the NSThread class:

1) [NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];

2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:)

object:nil];

[myThread start]; // Actually create the thread

Page 10: Aleksandr_Butenko_Mobile_Development

POSIX threads

POSIX threads provide a C-based interface for creating threads.

This technology can actually be used in any type of application (including Cocoa and Cocoa Touch applications) and might be more convenient if you are writing your software for multiple platforms. The POSIX routine you use to create threads is called, appropriately enough, pthread_create.

Page 11: Aleksandr_Butenko_Mobile_Development
Page 12: Aleksandr_Butenko_Mobile_Development

Thread entry routine

§ Creating an Autorelease Pool

§ Setting Up an Exception Handler

§ Setting Up a Run Loop

Page 13: Aleksandr_Butenko_Mobile_Development

Runloop

Page 14: Aleksandr_Butenko_Mobile_Development

Input sources

§ Port-Based Sources

§ Custom Input Sources

§ Cocoa Perform Selector Sources

Page 15: Aleksandr_Butenko_Mobile_Development
Page 16: Aleksandr_Butenko_Mobile_Development

Synchronization

§ Atomic Operations

§ Memory Barriers and Volatile Variables

§ Locks

§ Conditions

§ Perform Selector Routines

§ Locks

Page 17: Aleksandr_Butenko_Mobile_Development

Atomic operations

Atomic operations are a simple form of synchronization that work on simple data types. The advantage of atomic operations is that they do not block competing threads. For simple operations, such as incrementing a counter variable, this can lead to much better performance than taking a lock.

Page 18: Aleksandr_Butenko_Mobile_Development

Memory barriers and Volatile variables

§ A memory barrier acts like a fence, forcing the processor to complete any load and store operations positioned in front of the barrier before it is allowed to perform load and store operations positioned after the barrier.

§ Applying the volatile keyword to a variable forces the compiler to load that variable from memory each time it is used.

Page 19: Aleksandr_Butenko_Mobile_Development

Locks

§ Mutex

§ Recursive lock

§ Read-write lock

§ Distributed lock

§ Spin lock

§ Double-checked lock

Page 20: Aleksandr_Butenko_Mobile_Development

Conditions

A condition is another type of semaphore that allows threads to signal each other when a certain condition is true. Conditions are typically used to indicate the availability of a resource or to ensure that tasks are performed in a specific order. When a thread tests a condition, it blocks unless that condition is already true.

Page 21: Aleksandr_Butenko_Mobile_Development

Interthread Communication§ Direct messaging

§ Global variables, shared memory, and objects

§ Conditions

§ Run loop sources

§ Ports and sockets

§ Message queues

§ Cocoa distributed objects

Page 22: Aleksandr_Butenko_Mobile_Development

Operation objects § Operation object is a wrapper for a

task that would normally be executed on a secondary thread.

§ This wrapper hides the thread management aspects of performing the task, leaving you free to focus on the task itself.

Page 23: Aleksandr_Butenko_Mobile_Development

Dispatch queues

§ With GCD, you define the task you want to perform and add it to a work queue, which handles the scheduling of your task on an appropriate thread.

§ Work queues take into account the number of available cores and the current load to execute your tasks more efficiently than you could do yourself using threads.