Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thread-Safety in Go - An Overlooked Concern

Af78adc6caf494f74c1e4fb10a15c74a?s=47 Preslav Mihaylov
April 24, 2021
71

Thread-Safety in Go - An Overlooked Concern

A talk on learning the fundamentals of thread-safety in Go as presented on The Go Conference, Japan (https://gocon.jp/)

Materials - http://bit.ly/goconjp-thread-safety

Af78adc6caf494f74c1e4fb10a15c74a?s=128

Preslav Mihaylov

April 24, 2021
Tweet

Transcript

  1. Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer,

    Tech. Trainer, Speaker preslav@pmihaylov.com Go Conference Tokyo 2021, A random zoom room on the Internet
  2. 👋 こんにちは

  3. None
  4. I like 󰞵ing & 󰳓ing

  5. Check out my work @ github.com/preslavmihaylov, pmihaylov.com

  6. Why not a talk on concurrency?

  7. Most courses/books are incomplete…

  8. Not enough ❤ for thread-safety

  9. Concurrency without thread-safety -> 200 mp/h 🏎 without safety measures

  10. Goals → 1) Learn the fundamentals 2) Get guidance on

    next steps
  11. What’s covered 1. What does “thread-safety” mean? 2. Should you

    care about it at all? 3. Thread-safety problems & solutions a. When is there a potential thread-safety issue? b. Atomic access to shared state c. Delegating thread-safety d. Atomic compound actions e. Visibility & stale data 4. Where to go from here?
  12. What does “thread-safety” mean?

  13. Safe classes → behave correctly

  14. Thread-safe classes → behave correctly while bombarded by a gazillion

    goroutines
  15. Should you care about it at all?

  16. “I don’t need thread-safe classes as I’m typically writing web

    apps without spawning any threads…” the average web dev
  17. You’re already writing multi-threaded applications...

  18. ...and no. Channels can’t save you every time

  19. None
  20. Thread-safety problems & solutions

  21. When is there a potential thread-safety issue?

  22. None
  23. Is it thread-safe? Join at slido.com #gocon21s

  24. None
  25. None
  26. None
  27. Is it thread-safe? Join at slido.com #gocon21s

  28. Shared mutable state

  29. Non-shared state Stack #1 Thread #1 Non-shared state #1 Non-shared

    state #2 Shared memory aka heap Shared state Non-shared state Stack #2 Thread #2
  30. Atomic access to shared state

  31. None
  32. None
  33. What can go wrong?

  34. None
  35. Using Mutexes is Go’s standard way to add critical sections/locks

  36. Delegating thread-safety

  37. None
  38. None
  39. Delegated thread-safety → relying on the thread-safetiness of objects you

    use
  40. But also (third-party) packages...

  41. Atomic compound actions

  42. None
  43. None
  44. Actions from the same invariant need to be synchronized together

  45. Visibility & stale data

  46. None
  47. None
  48. Is it thread-safe? Join at slido.com #gocon21s

  49. Thread-safety is not only about concurrent writes...

  50. None
  51. Remember 3 things

  52. 0

  53. Stateless methods → thread-safe methods

  54. None
  55. 1

  56. Thread-safety is about both reads and writes...

  57. None
  58. 2

  59. Prefer delegation over explicit synchronization

  60. None
  61. 3

  62. Be careful with compound actions...

  63. None
  64. None
  65. Is that all I need to know?

  66. What’s not covered 1. Initialisation safety & Immutable objects 2.

    Thread-confinement 3. Concurrency tooling - atomic variables/collections, synchronizers 4. Goroutine Management 5. Channels 6. Managing goroutine lifecycle via cancellation/interrupts 7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.) 8. The Go Memory Model
  67. None
  68. Get the mats → http://bit.ly/goconjp-thread-safety

  69. Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer,

    Tech. Trainer, Speaker preslav@pmihaylov.com Go Conference Tokyo 2021, A random zoom room on the Internet