Thread Management - Ios (30mins)
Thread Management - Ios (30mins)
THREAD (8MINS)
INTRO
MEMORY COST
3 THREAD INTRO
Every application starts with a single thread, which runs the application's main
function. Applications can spawn additional threads, each of which executes the
code of a specific function
When an application spawns a new thread, that thread becomes an independent
entity inside of the application's process space. Each thread has its own execution
stack and is scheduled for runtime separately by the kernel. A thread can
communicate with other threads and other processes, perform I/O operations, and
do anything else you might need it to do. Because they are inside the same
process space, however, all threads in a single application share the same virtual
memory space and have the same access rights as the process itself.
4 THREAD COST
Threading has a real cost to your program (and the system) in terms of memory use and
performance. Each thread requires the allocation of memory in both the kernel memory
space and your programs memory space.
When an application spawns a new thread, that thread becomes an independent entity
inside of the application's process space. Each thread has its own execution stack and is
scheduled for runtime separately by the kernel. A thread can communicate with other
threads and other processes, perform I/O operations, and do anything else you might need
it to do. Because they are inside the same process space, however, all threads in a single
application share the same virtual memory space and have the same access rights as the
process itself.
Another cost to consider when writing threaded code is the production costs.
5 RUN LOOP
When writing code you want to run on a separate thread, you have two
options
The first option is to write the code for a thread as one long task to be
performed with little or no interruption, and have the thread exit when
it finishes.
The second option is put your thread into a loop and have it process
requests dynamically as they arrive.
The first option requires no special setup for your code; you just start
doing the work you want to do. The second option, however, involves
setting up your threads run loop.
8 DISPATCH QUEUES
Serial:
Serial queues execute one task at a time in the order in which they are added to
the queue.
Concurrent:
Concurrent queues (also known as a type of global dispatch queue) execute one
or more tasks concurrently, but tasks are still started in the order in which they
were added to the queue.
Main dispatch queue:
The main dispatch queue is a globally available serial queue that executes tasks
on the applications main thread.
10