0% found this document useful (0 votes)
52 views

Thread Management - Ios (30mins)

A short presentation on NSThread, GSD and NSOperationQueue

Uploaded by

prabindatta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Thread Management - Ios (30mins)

A short presentation on NSThread, GSD and NSOperationQueue

Uploaded by

prabindatta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Copyright IBM India Private Limited @ 2016, Prabin K Datta

1 THREAD MANAGEMENT iOS (30MINS)

Copyright IBM India Private Limited @ 2016, Prabin K Datta

THREAD (8MINS)
INTRO
MEMORY COST

Copyright IBM India Private Limited @ 2016, Prabin K Datta

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.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

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.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

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.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

GRAND CENTRAL DISPATCH


(12MINS)
MOVE AWAY FROM THREADS
DISPATCH QUEUES
DISPATCH QUEUES AND THREAD SAFETY

Copyright IBM India Private Limited @ 2016, Prabin K Datta

7 MOVE AWAY FROM THREADS


Specifically, using dispatch queues and operation queues instead of
threads has several advantages:
It reduces the memory penalty your application pays for storing thread
stacks in the applications memory space.
It eliminates the code needed to create and configure your threads.
It eliminates the code needed to manage and schedule work on
threads.
It simplifies the code you have to write.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

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.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

9 DISPATCH QUEUES AND THREAD SAFETY


Any time you are implementing concurrency in your application, there are a few things you should know:
Dispatch queues themselves are thread safe. In other words, you can submit tasks to a dispatch queue
from any thread on the system without first taking a lock or synchronizing access to the queue.
Do not call the
dispatch_sync function from a task that is executing on the same queue that you pass to your function ca
ll. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously
using the
dispatch_async function.
Avoid taking locks from the tasks you submit to a dispatch queue. Although it is safe to use locks from
your tasks, when you acquire the lock, you risk blocking a serial queue entirely if that lock is
unavailable. Similarly, for concurrent queues, waiting on a lock might prevent other tasks from
executing instead. If you need to synchronize parts of your code, use a serial dispatch queue instead of
a lock.
Although you can obtain information about the underlying thread running a task, it is better to avoid
doing so.

Copyright IBM India Private Limited @ 2016, Prabin K Datta

10

QUESTIONS & ANSWERS (10MINS)


Thread
GCD & NSOperation

You might also like