SwiftConf 2016: Concurrency on iOS

Ddd6d3bac7772fa67fc5e312a18bdaec?s=47 sammyd
September 08, 2016

SwiftConf 2016: Concurrency on iOS

...queueing for the futures

Some slides that accompany a talk presented at SwiftConf 2016. You should check out the Xcode Playground to get a better understanding of what the hell is going on

Ddd6d3bac7772fa67fc5e312a18bdaec?s=128

sammyd

September 08, 2016
Tweet

Transcript

  1. Concurrency on iOS Sam Davies @iwantmyrealname _ github.com/sammyd
 git.io/vicO3

  2. concurrency is hard

  3. what is concurrency?

  4. why use concurrency?

  5. common concurrency problems

  6. race condition

  7. Race Condition time Value 1 Thread 1 Thread 2 inc

  8. Race Condition time Value 1 1 Thread 1 Thread 2

    inc r1
  9. Race Condition time Value 1 1 1 Thread 1 Thread

    2 inc r1 +1
  10. Race Condition time Value 1 1 1 2 Thread 1

    Thread 2 inc r1 w2 +1
  11. Race Condition time Value 1 1 1 2 2 Thread

    1 Thread 2 inc r1 w2 +1 inc
  12. Race Condition time Value 1 1 1 2 2 2

    Thread 1 Thread 2 inc r1 w2 +1 inc r2
  13. Race Condition time Value 1 1 1 2 2 2

    2 Thread 1 Thread 2 inc r1 w2 +1 inc r2 +1
  14. Race Condition time Value 1 1 1 2 2 2

    2 3 Thread 1 Thread 2 inc r1 w2 +1 inc r2 w3 +1
  15. Race Condition time Value 1 Thread 1 Thread 2 inc

  16. Race Condition time Value 1 1 Thread 1 Thread 2

    inc r1 inc
  17. Race Condition time Value 1 1 1 Thread 1 Thread

    2 inc r1 inc r1
  18. Race Condition time Value 1 1 1 1 Thread 1

    Thread 2 inc r1 inc r1 +1
  19. Race Condition time Value 1 1 1 2 1 Thread

    1 Thread 2 inc r1 inc r1 w2 +1
  20. Race Condition time Value 1 1 1 2 1 2

    Thread 1 Thread 2 inc r1 +1 inc r1 w2 +1
  21. Race Condition time Value 1 1 1 2 1 2

    2 Thread 1 Thread 2 inc r1 w2 +1 inc r1 w2 +1
  22. priority inversion

  23. Priority Inversion time low priority resource

  24. Priority Inversion time low priority resource

  25. Priority Inversion time low medium priority resource

  26. Priority Inversion time low medium high priority resource

  27. Priority Inversion time low medium high priority resource

  28. Priority Inversion time low medium high priority resource

  29. Priority Inversion time low medium high priority resource

  30. deadlock

  31. Deadlock time resource1 resource2 Thread 1 Thread 2

  32. Deadlock time resource1 resource2 Thread 1 Thread 2

  33. Deadlock time resource1 resource2 Thread 1 Thread 2

  34. Deadlock time resource1 resource2 Thread 1 Thread 2

  35. Deadlock time resource1 resource2 Thread 1 Thread 2

  36. Deadlock time resource1 resource2 Thread 1 Thread 2

  37. Deadlock time resource1 resource2 Thread 1 Thread 2

  38. Deadlock time resource1 resource2 Thread 1 Thread 2

  39. $&'( queueueueueue paradigm

  40. Tasks & Queues Thread 1 Thread 2 Queue

  41. Tasks & Queues Thread 1 Thread 2 Queue

  42. Tasks & Queues Thread 1 Thread 2 Queue

  43. Tasks & Queues Thread 1 Thread 2 Queue

  44. Tasks & Queues Thread 1 Thread 2 Queue

  45. Tasks & Queues Thread 1 Thread 2 Queue

  46. Tasks & Queues Thread 1 Thread 2 Queue

  47. feeling brave?

  48. a touch more theory

  49. Thread Safety time Value 1111 Thread 1 Thread 2 Thread

    3
  50. Thread Safety time Value 1111 Thread 1 Thread 2 r1111

    Thread 3
  51. Thread Safety time Value 1111 1112 Thread 1 Thread 2

    r1111 w2222 Thread 3
  52. Thread Safety time Value 1111 1112 1122 Thread 1 Thread

    2 r1111 w2222 r1122 Thread 3
  53. Thread Safety time Value 1111 1112 1122 1223 Thread 1

    Thread 2 r1111 w2222 r1122 w3333 Thread 3 r1223
  54. Thread Safety time Value 1111 1112 1122 1223 2233 Thread

    1 Thread 2 r1111 w2222 r1122 w3333 Thread 3 r1223
  55. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223
  56. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    3333 Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223
  57. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    3333 3333 3333 Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223 r3333 r3333
  58. Dispatch Barrier time task task task task task barrier task

    task task task task task task task
  59. Dispatch Barrier time read read read read read read write

    read read read read read read
  60. alternatives to 

  61. promises networkRequest(url, callback: { data in decompressor(data, callback: { image

    in imageResizer(image, callback: { small in filterImage(small, callback: { filtered in displayImage(filtered) }) }) }) })
  62. promises promise<int> int error

  63. promises str

  64. promises promise<img> str f

  65. promises promise<img> str error f img

  66. promises promise<img> str error f img error handler

  67. promises promise<img> str error f img g promise<img> error handler

  68. promises promise<img> str error f img g promise<img> error error

    handler img
  69. promises promise<img> str error f img g promise<img> error error

    handler img
  70. promises networkRequest(url) .then(decompressor) .then(imageResizer) .then({ (image) in return filterImage(image) }).then(displayImage)

    .catch({ (error) in print(error) })
  71. imagine the “promise pipeline” accepted a sequence of values…

  72. that’s reactive programming

  73. conclusion

  74. concurrency is hard

  75. you will get it wrong

  76. • videos.raywenderlich.com • git.io/vicO3 • @iwantmyrealname