45
Introducing Concurrency Introduction

Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Intr

oduc

ing

ConcurrencyIntroduction

Page 2: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Concurrencyis

HARD

Page 3: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

What is Concurrency?

Multiple tasks taking place simultaneously Utilises multi-core CPUs Can make apps much more responsive Traditionally uses a threading model

Page 4: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Issues with multi-threading

Easy to get wrong Difficult to debug Specific scenarios Race condition Priority inversion Deadlock

Page 5: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1

Thread 1

Thread 2

inc

Page 6: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1

Thread 1

Thread 2

inc r1

Page 7: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1

Thread 1

Thread 2

inc r1 +1

Page 8: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 2

Thread 1

Thread 2

inc r1 w2+1

Page 9: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 2 2

Thread 1

Thread 2

inc r1 w2+1

inc

Page 10: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 2 2 2

Thread 1

Thread 2

inc r1 w2+1

inc r2

Page 11: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 2 2 2 2

Thread 1

Thread 2

inc r1 w2+1

inc r2 +1

Page 12: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 2 2 2 2 3

Thread 1

Thread 2

inc r1 w2+1

inc r2 w3+1

Page 13: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1

Thread 1

Thread 2

inc

Page 14: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1

Thread 1

Thread 2

inc r1

inc

Page 15: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1

Thread 1

Thread 2

inc r1

inc r1

Page 16: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 1

Thread 1

Thread 2

inc r1

inc r1 +1

Page 17: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 1 2

Thread 1

Thread 2

inc r1

inc r1 w2+1

Page 18: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 1 2 2

Thread 1

Thread 2

inc r1 +1

inc r1 w2+1

Page 19: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Race Condition

time

Value 1 1 1 1 2 2 2

Thread 1

Thread 2

inc r1 w2+1

inc r1 w2+1

Page 20: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

priority

resource

Page 21: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

priority

resource 🔒

Page 22: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

medium

priority

resource 🔒

Page 23: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

medium

highpriority

resource 🔒

Page 24: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

medium

highpriority

resource 🔒

Page 25: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

medium

highpriority

resource 🔒

Page 26: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Priority Inversion

time

low

medium

highpriority

resource 🔒 🔒

Page 27: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

Page 28: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

Page 29: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

Page 30: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

🔒

Page 31: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

🔒

Page 32: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

🔒

Page 33: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

🔒

Page 34: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Deadlock

timeresource1

resource2

Thread 1

Thread 2

🔒

🔒

Page 35: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Simpler abstraction Scales with system and conditions Lower overhead Serial queues include intrinsic locking

Page 36: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 37: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 38: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 39: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 40: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 41: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 42: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Tasks & Queues

Thread 1

Thread 2

Queue

Page 43: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Act I: NSOperation

NSOperation NSOperationQueue Async Operations Dependencies Cancellation NSOperation in Practice

Page 44: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Act II: Grand Central Dispatch

Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights

Page 45: Introduction - raywenderlich.com...Act II: Grand Central Dispatch Fire & Forget with GCD Dispatch Groups Thread-safe resources GCD Delights. Where to go from here? "! Title: 000_Introduction.key

Where to go from here?

📼⌀⌁⌂⌃⌄⌅⌆⌇⌈⌉⌊⌋⌌⌍⌎⌏⌐⌑⌒⌓⌔⌕⌖⌗⌘⌙⌚⌛⌜⌝⌞⌟⌠⌡⌢⌣⌤⌥⌦⌧⌨⟨⟩⌫⌬⌭⌮⌯⌰⌱⌲⌳⌴⌵⌶⌷⌸⌹⌺⌻⌼⌽⌾⌿⍀⍁⍂⍃⍄⍅⍆⍇⍈⍉⍊⍋⍌⍍⍎⍏⍐⍑⍒⍓⍔⍕⍖⍗⍘⍙⍚⍛⍜⍝⍞⍟⍠⍡⍢⍣⍤⍥⍦⍧⍨⍩⍪⍫⍬⍭⍮⍯⍰⍱⍲⍳⍴⍵⍶⍷⍸⍹⍺⍻⍼⍽⍾⍿⎀⎁⎂⎃⎄⎅⎆⎇⎈⎉⎊⎋⎌⎍⎎⎏⎐⎑⎒⎓⎔⎕⎖⎗⎘⎙⎚⎛⎜⎝⎞⎟⎠⎡⎢⎣⎤⎥⎦⎧⎨⎩⎪⎫⎬⎭⎮⎯⎰⎱⎲⎳⎴⎵⎶⎷⎸⎹⎺⎻⎼⎽⎾⎿⏀⏁⏂⏃⏄⏅⏆⏇⏈⏉⏊⏋⏌⏍⏎⏏⏐⏑⏒⏓⏔⏕⏖⏗⏘⏙⏚⏛⏜⏝⏞⏟⏠⏡⏢⏣⏤⏥⏦⏧⏨⏩⏪⏫⏬⏭⏮⏯⏰⏱⏲⏳