SlideShare a Scribd company logo
Grand Central Dispatch
and multi-threading
Kuba Brecka
@kubabrecka
Terminology
• Parallel
• Multi-threaded
• Concurrent
• Simultaneous
• Asynchronous
Why parallelize?
• Responsiveness
• “when I scroll, it’s smooth”
• Performance
• “it works fast”
• Energy saving
• “it doesn’t drain my battery”
• Convenience
• some things are parallel by nature, e.g. running two completely
separate apps
How?
• Multiple processes
• XPC, fork
• Multiple threads
• POSIX Threads, NSThread
• High-level thread abstraction
• Operation queues, dispatch queues
• GPGPU
• Instruction-level parallelism
• superscalar CPUs, pipelining, vector instructions
• Multiple PCs
• servers, clouds
Threads
• What is a thread?
• It’s an abstraction made by the OS
• The CPU has no such concept
• Represents a line of calculation
• Has an ID, a stack, thread-local storage, priority, CPU registers
• Shares memory and resources within a process
• The OS scheduler runs/pauses threads
• context switching
Issues with threading
• Race conditions
• the result depends on the timing of the scheduler
• the behavior is non-deterministic
• can result in almost anything
• crash, wrong result, corrupted data
• So, you have to use locks/mutexes/…
• More issues: deadlocks, livelocks, starvation
• Even the best guys have trouble with these
• Security consequences, vulnerabilities
Know your enemy
• The compiler
• The CPU
• The memory
• Time
• Your brain
The iPhone has matured
iPhone 4
512 MB RAM
A4 SoC (1 core)
800 MHz
iPhone 4S
512 MB RAM
A5 SoC (2 core)
800 MHz
iPhone 5
1 GB RAM
A6 SoC (2 core)
1300 MHz
iPhone 5S
1 GB RAM
A7 SoC (2 core)
1300 MHz
ARM has matured
• Apple A5 (2011)
• ARM Cortex-A9 MPCore
• 2 cores
• out-of-order execution
• speculative execution
• superscalar, pipelining (8 stages)
• NEON 128-bit SIMD
• Apple A7 (2013)
• ARMv8-A “Cyclone”
• 64-bit, 32 registers, per-core L1 cache
iOS has matured
• The kernel knows a lot more about the system than
the developer
• GCD
• Operation Queues
• LLVM, compiler optimizations
• GPU computations
• Accelerate.framework
iOS threading technologies
• Multiple processes – forking disabled, no XPC
• Low-level threads
• POSIX Threads (pthread)
• NSThread
• -[NSObject performSelectorInBackground:withObject:]
• Higher-level abstractions
• NSOperationQueue, NSOperation
• GCD
Parallelizing tasks vs.
algorithms
• Task = a standalone unit of work
• has some inputs, gives some outputs
• “add a blur effect to these 1000 photos”
• 1 photo = 1 task (independent)
• “add a blur effect to this one 5000x5000px photo”
• 1 task = ?
• Some algorithms simply cannot be parallelized (you will not get
any significant speedup)
What’s thread safety?
• “Thread-safe object”
• you can safely use the object from multiple threads at the
same time
• the internal state of the object will not get corrupted and it will
behave correctly
• When you don’t know if an object is thread-safe, you have to
assume it isn’t
• How do you make your object thread-safe?
• immutability, locks, atomic reads/writes
Shared mutable state
• Exclusive immutable object = no problem
• Shared immutable object = no problem
• Exclusive mutable object = no problem
• Shared mutable object
• root of all evil
• you always want to minimize this
Global variables
• “Global variables are bad”
• Multi-threading is another very good reason not to
use global variables / global state
• Global variables are always shared
• Watch out for “hidden” global state:
• working directory, chdir()
• environment variables, putenv()
Thread-safety vs. iOS
• Terrible lack of proper documentation
• Most of the low-level Obj-C runtime is thread-safe
• memory management, ARC, weak references, …
• Immutable objects (NSString, NSArray, …) are thread-safe
• A few other classes are thread-safe
• Usually it’s thread-safe to call class methods
• google for “iOS thread safety”
• https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/
Conceptual/Multithreading/ThreadSafetySummary/
ThreadSafetySummary.html
Grand Central Dispatch
• Let’s not think about threads
• Instead, let’s think about tasks
• New concepts:
• Tasks
• Queues
• Queue-specific data
• Dispatch groups
• Dispatch sources
• Synchronization
• Semaphores, barriers
• C API (!) but has ARC and works with blocks
GCD queues
• Main queue
• there is just one, executed on the main thread
• Concurrent queue
• tasks run concurrently
• 4 pre-made concurrent queues with different priorities
• DISPATCH_QUEUE_PRIORITY_DEFAULT, _HIGH, _LOW, _BACKGROUND
• you can make your own
• Serial queue
• only one task at a time, in order
• you can make your own
GCD task API
• Get/create a queue:
• dispatch_get_global_queue
• dispatch_get_main_queue
• dispatch_queue_create
• Submit task:
• dispatch_sync
• dispatch_async
• dispatch_apply
GCD convenience API
• dispatch_once
• guarantees the code run only run once
• use to implement a proper and fast singleton
• dispatch_after
• execute the task at a specific time
It’s not threads
• GCD uses threads, but the threads are completely
managed by GCD
• You can’t assume your code will run on any specific
thread
• even two tasks from the same serial queue can run
on different threads
• Don’t use thread-local storage
• Don’t use thread priorities
Demo
Solutions
• Avoid shared mutable state
• communicate by message passing
• design your objects as immutable
• avoid multithreading
• Synchronization
• You must always have “a plan”
• if you can’t tell which code is supposed to run in which thread/queue, then
nobody can help you
• if you can’t tell which data can be accessed from which thread/queue, then
nobody can help you
• Thank you.
Kuba Brecka
@kubabrecka
iosak.cz

More Related Content

What's hot (20)

PPTX
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Adler Hsieh
 
PPT
Introduction to node.js aka NodeJS
JITENDRA KUMAR PATEL
 
KEY
Performance and scalability with drupal
Ronan Berder
 
PDF
Data Processing and Ruby in the World
SATOSHI TAGOMORI
 
PDF
HPCC Systems Engineering Summit Presentation - Leveraging HPCC Systems with V...
HPCC Systems
 
PDF
Introduction to Node.js
Richard Lee
 
PDF
Kubernetes
Sang-Min Park
 
PPTX
My first powershell script
David Cobb
 
PDF
Introduction to Cassandra - Denver
Jon Haddad
 
PDF
PG Day'14 Russia, Secure PostgreSQL Deployment, Magnus Hagander
pgdayrussia
 
PPT
5 Common Mistakes You are Making on your Website
Acquia
 
PDF
Big Data! Great! Now What? #SymfonyCon 2014
Ricard Clau
 
PDF
Applying the paradigms of core.async in Clojure and ClojureScript
Julian Gamble
 
PPTX
Maven
Khan625
 
PDF
Stackato v3
Jonas Brømsø
 
PDF
Modern software architectures - PHP UK Conference 2015
Ricard Clau
 
PDF
Crash course intro to cassandra
Jon Haddad
 
PPTX
Building and Managing Projects with Maven
Khan625
 
PDF
Building a bakery of Windows servers with Packer - London WinOps
Ricard Clau
 
PPTX
CQ5 Development Setup, Maven Build and Deployment
klcodanr
 
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Adler Hsieh
 
Introduction to node.js aka NodeJS
JITENDRA KUMAR PATEL
 
Performance and scalability with drupal
Ronan Berder
 
Data Processing and Ruby in the World
SATOSHI TAGOMORI
 
HPCC Systems Engineering Summit Presentation - Leveraging HPCC Systems with V...
HPCC Systems
 
Introduction to Node.js
Richard Lee
 
Kubernetes
Sang-Min Park
 
My first powershell script
David Cobb
 
Introduction to Cassandra - Denver
Jon Haddad
 
PG Day'14 Russia, Secure PostgreSQL Deployment, Magnus Hagander
pgdayrussia
 
5 Common Mistakes You are Making on your Website
Acquia
 
Big Data! Great! Now What? #SymfonyCon 2014
Ricard Clau
 
Applying the paradigms of core.async in Clojure and ClojureScript
Julian Gamble
 
Maven
Khan625
 
Stackato v3
Jonas Brømsø
 
Modern software architectures - PHP UK Conference 2015
Ricard Clau
 
Crash course intro to cassandra
Jon Haddad
 
Building and Managing Projects with Maven
Khan625
 
Building a bakery of Windows servers with Packer - London WinOps
Ricard Clau
 
CQ5 Development Setup, Maven Build and Deployment
klcodanr
 

Viewers also liked (7)

PDF
Distributed Multi-Threading in GNU-Prolog
Nuno Morgadinho
 
PPT
Architectural cncepts: Chip Multithreaded Era
Marcelo Arbore
 
PDF
Ladění iOS aplikací nejen v Xcode [For Mobile červen 2013]
Kuba Břečka
 
PDF
Dark Side of iOS [SmartDevCon 2013]
Kuba Břečka
 
PDF
Úvod do vývoje pro platformu iOS [ZČU 24.4.2014]
Kuba Břečka
 
PDF
Dark Side of iOS [mDevCamp 2013]
Kuba Břečka
 
PPT
Computer architecture
Rishabha Garg
 
Distributed Multi-Threading in GNU-Prolog
Nuno Morgadinho
 
Architectural cncepts: Chip Multithreaded Era
Marcelo Arbore
 
Ladění iOS aplikací nejen v Xcode [For Mobile červen 2013]
Kuba Břečka
 
Dark Side of iOS [SmartDevCon 2013]
Kuba Břečka
 
Úvod do vývoje pro platformu iOS [ZČU 24.4.2014]
Kuba Břečka
 
Dark Side of iOS [mDevCamp 2013]
Kuba Břečka
 
Computer architecture
Rishabha Garg
 
Ad

Similar to Grand Central Dispatch and multi-threading [iCONdev 2014] (20)

PDF
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
 
PPT
JavaScript Event Loop
Thomas Hunter II
 
PDF
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Nexus FrontierTech
 
PDF
Ruby and Distributed Storage Systems
SATOSHI TAGOMORI
 
PPTX
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
PPTX
Overview of Message Queues
Bozhidar Bozhanov
 
PPT
Node and Azure
Jason Gerard
 
PDF
Bringing Concurrency to Ruby - RubyConf India 2014
Charles Nutter
 
PPTX
Devnexus 2018
Roy Russo
 
PPTX
Dev nexus 2017
Roy Russo
 
PPTX
NodeJS Concurrency
pgriess
 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
 
PDF
New hope is comming? Project Loom.pdf
Krystian Zybała
 
PPTX
Optimizing Java Notes
Adam Feldscher
 
PDF
Java in High Frequency Trading
Viktor Sovietov
 
PDF
Caching your rails application
ArrrrCamp
 
PPTX
Drupal performance
Piyuesh Kumar
 
PDF
Store
ESUG
 
PPTX
Thread
Syed Zaid Irshad
 
PDF
End-to-end W3C APIs - tpac 2012
Alexandre Morgaut
 
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
 
JavaScript Event Loop
Thomas Hunter II
 
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Nexus FrontierTech
 
Ruby and Distributed Storage Systems
SATOSHI TAGOMORI
 
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
Overview of Message Queues
Bozhidar Bozhanov
 
Node and Azure
Jason Gerard
 
Bringing Concurrency to Ruby - RubyConf India 2014
Charles Nutter
 
Devnexus 2018
Roy Russo
 
Dev nexus 2017
Roy Russo
 
NodeJS Concurrency
pgriess
 
NodeJS - Server Side JS
Ganesh Kondal
 
New hope is comming? Project Loom.pdf
Krystian Zybała
 
Optimizing Java Notes
Adam Feldscher
 
Java in High Frequency Trading
Viktor Sovietov
 
Caching your rails application
ArrrrCamp
 
Drupal performance
Piyuesh Kumar
 
Store
ESUG
 
End-to-end W3C APIs - tpac 2012
Alexandre Morgaut
 
Ad

Recently uploaded (20)

PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

Grand Central Dispatch and multi-threading [iCONdev 2014]

  • 1. Grand Central Dispatch and multi-threading Kuba Brecka @kubabrecka
  • 2. Terminology • Parallel • Multi-threaded • Concurrent • Simultaneous • Asynchronous
  • 3. Why parallelize? • Responsiveness • “when I scroll, it’s smooth” • Performance • “it works fast” • Energy saving • “it doesn’t drain my battery” • Convenience • some things are parallel by nature, e.g. running two completely separate apps
  • 4. How? • Multiple processes • XPC, fork • Multiple threads • POSIX Threads, NSThread • High-level thread abstraction • Operation queues, dispatch queues • GPGPU • Instruction-level parallelism • superscalar CPUs, pipelining, vector instructions • Multiple PCs • servers, clouds
  • 5. Threads • What is a thread? • It’s an abstraction made by the OS • The CPU has no such concept • Represents a line of calculation • Has an ID, a stack, thread-local storage, priority, CPU registers • Shares memory and resources within a process • The OS scheduler runs/pauses threads • context switching
  • 6. Issues with threading • Race conditions • the result depends on the timing of the scheduler • the behavior is non-deterministic • can result in almost anything • crash, wrong result, corrupted data • So, you have to use locks/mutexes/… • More issues: deadlocks, livelocks, starvation • Even the best guys have trouble with these • Security consequences, vulnerabilities
  • 7. Know your enemy • The compiler • The CPU • The memory • Time • Your brain
  • 8. The iPhone has matured iPhone 4 512 MB RAM A4 SoC (1 core) 800 MHz iPhone 4S 512 MB RAM A5 SoC (2 core) 800 MHz iPhone 5 1 GB RAM A6 SoC (2 core) 1300 MHz iPhone 5S 1 GB RAM A7 SoC (2 core) 1300 MHz
  • 9. ARM has matured • Apple A5 (2011) • ARM Cortex-A9 MPCore • 2 cores • out-of-order execution • speculative execution • superscalar, pipelining (8 stages) • NEON 128-bit SIMD • Apple A7 (2013) • ARMv8-A “Cyclone” • 64-bit, 32 registers, per-core L1 cache
  • 10. iOS has matured • The kernel knows a lot more about the system than the developer • GCD • Operation Queues • LLVM, compiler optimizations • GPU computations • Accelerate.framework
  • 11. iOS threading technologies • Multiple processes – forking disabled, no XPC • Low-level threads • POSIX Threads (pthread) • NSThread • -[NSObject performSelectorInBackground:withObject:] • Higher-level abstractions • NSOperationQueue, NSOperation • GCD
  • 12. Parallelizing tasks vs. algorithms • Task = a standalone unit of work • has some inputs, gives some outputs • “add a blur effect to these 1000 photos” • 1 photo = 1 task (independent) • “add a blur effect to this one 5000x5000px photo” • 1 task = ? • Some algorithms simply cannot be parallelized (you will not get any significant speedup)
  • 13. What’s thread safety? • “Thread-safe object” • you can safely use the object from multiple threads at the same time • the internal state of the object will not get corrupted and it will behave correctly • When you don’t know if an object is thread-safe, you have to assume it isn’t • How do you make your object thread-safe? • immutability, locks, atomic reads/writes
  • 14. Shared mutable state • Exclusive immutable object = no problem • Shared immutable object = no problem • Exclusive mutable object = no problem • Shared mutable object • root of all evil • you always want to minimize this
  • 15. Global variables • “Global variables are bad” • Multi-threading is another very good reason not to use global variables / global state • Global variables are always shared • Watch out for “hidden” global state: • working directory, chdir() • environment variables, putenv()
  • 16. Thread-safety vs. iOS • Terrible lack of proper documentation • Most of the low-level Obj-C runtime is thread-safe • memory management, ARC, weak references, … • Immutable objects (NSString, NSArray, …) are thread-safe • A few other classes are thread-safe • Usually it’s thread-safe to call class methods • google for “iOS thread safety” • https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/ Conceptual/Multithreading/ThreadSafetySummary/ ThreadSafetySummary.html
  • 17. Grand Central Dispatch • Let’s not think about threads • Instead, let’s think about tasks • New concepts: • Tasks • Queues • Queue-specific data • Dispatch groups • Dispatch sources • Synchronization • Semaphores, barriers • C API (!) but has ARC and works with blocks
  • 18. GCD queues • Main queue • there is just one, executed on the main thread • Concurrent queue • tasks run concurrently • 4 pre-made concurrent queues with different priorities • DISPATCH_QUEUE_PRIORITY_DEFAULT, _HIGH, _LOW, _BACKGROUND • you can make your own • Serial queue • only one task at a time, in order • you can make your own
  • 19. GCD task API • Get/create a queue: • dispatch_get_global_queue • dispatch_get_main_queue • dispatch_queue_create • Submit task: • dispatch_sync • dispatch_async • dispatch_apply
  • 20. GCD convenience API • dispatch_once • guarantees the code run only run once • use to implement a proper and fast singleton • dispatch_after • execute the task at a specific time
  • 21. It’s not threads • GCD uses threads, but the threads are completely managed by GCD • You can’t assume your code will run on any specific thread • even two tasks from the same serial queue can run on different threads • Don’t use thread-local storage • Don’t use thread priorities
  • 22. Demo
  • 23. Solutions • Avoid shared mutable state • communicate by message passing • design your objects as immutable • avoid multithreading • Synchronization • You must always have “a plan” • if you can’t tell which code is supposed to run in which thread/queue, then nobody can help you • if you can’t tell which data can be accessed from which thread/queue, then nobody can help you
  • 24. • Thank you. Kuba Brecka @kubabrecka iosak.cz