Multitasking in iOS <ul><li>Alexander Butenko, Ciklum </li></ul>
What is a multitasking? <ul><li>§ Multitasking  is a method where multiple tasks share resources such as a  CPU . In the c...
Multitasking goals <ul><li>§ Improvement of application’s perceived responsiveness. </li></ul><ul><li>§ Improvement of app...
Basics <ul><li>To run program system need to make environment for task execution. This environment consists of memory reso...
Threads advantages <ul><li>§ Thread need less system resources (time for creation and physical resources) than processes <...
Threads disadvantages <ul><li>§ Thread  add uncertainty to your code </li></ul><ul><li>§  Threads still introduce a tremen...
Threads alternatives <ul><li>§ Operation objects </li></ul><ul><li>§ Grand Central Dispatch (GCD) </li></ul><ul><li>§ Idle...
Threads in iOS <ul><li>§  Сocoa threads </li></ul><ul><li>§ POSIX threads </li></ul>
Сocoa threads <ul><li>Cocoa implements threads using the  NSThread  class: </li></ul><ul><li>1)  [NSThread  detachNewThrea...
POSIX threads <ul><li>POSIX threads provide a C-based interface for creating threads.  </li></ul><ul><li>This technology c...
 
Thread entry routine §  Creating an Autorelease Pool § Setting Up an Exception Handler § Setting Up a Run Loop
Runloop
Input sources §  Port-Based Sources § Custom Input Sources § Cocoa Perform Selector Sources
 
Synchronization <ul><li>§  Atomic Operations </li></ul><ul><li>§ Memory Barriers and Volatile Variables </li></ul><ul><li>...
Atomic operations <ul><li>Atomic operations are a simple form of synchronization that work on simple data types. The advan...
Memory barriers and Volatile variables <ul><li>§  A memory barrier acts like a fence, forcing the processor to complete an...
Locks <ul><li>§  Mutex </li></ul><ul><li>§ Recursive lock </li></ul><ul><li>§ Read-write lock </li></ul><ul><li>§ Distribu...
Conditions <ul><li>A condition is another type of semaphore that allows threads to signal each other when a certain condit...
Interthread Communication <ul><li>§  Direct messaging </li></ul><ul><li>§ Global variables, shared memory, and objects </l...
Operation objects § Operation object is a wrapper for a task that would normally be executed on a secondary thread.  §  Th...
Dispatch queues §  With GCD, you define the task you want to perform and add it to a work queue, which handles the schedul...
Upcoming SlideShare
Loading in …5
×

Aleksandr_Butenko_Mobile_Development

1,437 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,437
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Посмотреть, что это за боги EventLoop
  • Пример с пареметризацией пункта меню
  • NSUndoManager почитать
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Посмотреть RunTime Programming Guide Посмотреть на временные задержки вызова операций На возврат результата из метода, инвокешна и селектора На сохранение инвокешна на диск или в память
  • Aleksandr_Butenko_Mobile_Development

    1. 1. Multitasking in iOS <ul><li>Alexander Butenko, Ciklum </li></ul>
    2. 2. What is a multitasking? <ul><li>§ 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. </li></ul>
    3. 3. Multitasking goals <ul><li>§ Improvement of application’s perceived responsiveness. </li></ul><ul><li>§ Improvement of application’s real-time performance on multicore systems. </li></ul>
    4. 4. Basics <ul><li>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 . </li></ul><ul><li>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. </li></ul>
    5. 5. Threads advantages <ul><li>§ Thread need less system resources (time for creation and physical resources) than processes </li></ul><ul><li>§ Interthread communication easier than interprocess communication </li></ul>
    6. 6. Threads disadvantages <ul><li>§ Thread add uncertainty to your code </li></ul><ul><li>§ Threads still introduce a tremendous amount of overhead to your process </li></ul>
    7. 7. Threads alternatives <ul><li>§ Operation objects </li></ul><ul><li>§ Grand Central Dispatch (GCD) </li></ul><ul><li>§ Idle-time notifications </li></ul><ul><li>§ Asynchronous functions </li></ul><ul><li>§ Timers </li></ul>
    8. 8. Threads in iOS <ul><li>§ Сocoa threads </li></ul><ul><li>§ POSIX threads </li></ul>
    9. 9. Сocoa threads <ul><li>Cocoa implements threads using the NSThread class: </li></ul><ul><li>1) [NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil]; </li></ul><ul><li>2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:) </li></ul><ul><li>object:nil]; </li></ul><ul><li>[myThread start]; // Actually create the thread </li></ul>
    10. 10. POSIX threads <ul><li>POSIX threads provide a C-based interface for creating threads. </li></ul><ul><li>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 . </li></ul>
    11. 12. Thread entry routine § Creating an Autorelease Pool § Setting Up an Exception Handler § Setting Up a Run Loop
    12. 13. Runloop
    13. 14. Input sources § Port-Based Sources § Custom Input Sources § Cocoa Perform Selector Sources
    14. 16. Synchronization <ul><li>§ Atomic Operations </li></ul><ul><li>§ Memory Barriers and Volatile Variables </li></ul><ul><li>§ Locks </li></ul><ul><li>§ Conditions </li></ul><ul><li>§ Perform Selector Routines </li></ul><ul><li>§ Locks </li></ul>
    15. 17. Atomic operations <ul><li>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. </li></ul>
    16. 18. Memory barriers and Volatile variables <ul><li>§ 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. </li></ul><ul><li>§ Applying the volatile keyword to a variable forces the compiler to load that variable from memory each time it is used. </li></ul>
    17. 19. Locks <ul><li>§ Mutex </li></ul><ul><li>§ Recursive lock </li></ul><ul><li>§ Read-write lock </li></ul><ul><li>§ Distributed lock </li></ul><ul><li>§ Spin lock </li></ul><ul><li>§ Double-checked lock </li></ul>
    18. 20. Conditions <ul><li>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. </li></ul>
    19. 21. Interthread Communication <ul><li>§ Direct messaging </li></ul><ul><li>§ Global variables, shared memory, and objects </li></ul><ul><li>§ Conditions </li></ul><ul><li>§ Run loop sources </li></ul><ul><li>§ Ports and sockets </li></ul><ul><li>§ Message queues </li></ul><ul><li>§ Cocoa distributed objects </li></ul>
    20. 22. 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.
    21. 23. 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.

    ×