aleksandr_butenko_mobile_development
TRANSCRIPT
![Page 1: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/1.jpg)
Multitasking in iOS
Alexander Butenko, Ciklum
![Page 2: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/3.jpg)
Multitasking goals
§ Improvement of application’s perceived responsiveness.
§ Improvement of application’s real-time performance on multicore systems.
![Page 4: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/7.jpg)
Threads alternatives
§ Operation objects
§ Grand Central Dispatch (GCD)
§ Idle-time notifications
§ Asynchronous functions
§ Timers
![Page 8: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/8.jpg)
Threads in iOS
§ Сocoa threads
§ POSIX threads
![Page 9: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/9.jpg)
С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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/11.jpg)
![Page 12: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/12.jpg)
Thread entry routine
§ Creating an Autorelease Pool
§ Setting Up an Exception Handler
§ Setting Up a Run Loop
![Page 13: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/13.jpg)
Runloop
![Page 14: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/14.jpg)
Input sources
§ Port-Based Sources
§ Custom Input Sources
§ Cocoa Perform Selector Sources
![Page 15: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/15.jpg)
![Page 16: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/16.jpg)
Synchronization
§ Atomic Operations
§ Memory Barriers and Volatile Variables
§ Locks
§ Conditions
§ Perform Selector Routines
§ Locks
![Page 17: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/19.jpg)
Locks
§ Mutex
§ Recursive lock
§ Read-write lock
§ Distributed lock
§ Spin lock
§ Double-checked lock
![Page 20: Aleksandr_Butenko_Mobile_Development](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062418/5563a774d8b42aae0d8b5034/html5/thumbnails/23.jpg)
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.