Update concurrency slides

This commit is contained in:
dwrz
2026-10-27 12:41:06 +00:00
parent 3dc29f36e5
commit 42c2105b16
+95 -82
View File
@@ -61,7 +61,7 @@ A second is a duration, not an instant.
Two things can happen in the same second and not be simultaneous. Two things can happen in the same second and not be simultaneous.
In computer science, this nuance matters. With computers, this nuance matters.
* 9. Time * 9. Time
@@ -98,10 +98,12 @@ Source: [[https://www.prowesscorp.com/computer-latency-at-a-human-scale/]]
* 12. Cooking Recipes * 12. Cooking Recipes
- Step-by-step - Step-by-step
- Concurrent - Concurrent:
- Pre-heat oven - Handle other tasks while:
- Boil water - Preheating oven
- Timers - Boiling water
- Waiting for timers
- Switching back to a task when it is ready for more work.
* 13. Multitasking * 13. Multitasking
@@ -117,24 +119,28 @@ Computers can "simultaneously" work on:
Combination of: Combination of:
- Operating System *Scheduler* - Operating System *Scheduler*
- Hardware (/interrupts/) - Hardware (/interrupts/)
- Keyboard
- Filesystem
- Network Card
- Timer
* 17. Cooperative Scheduling * 16. Cooperative Scheduling
[[file:images/cooperative-scheduling.jpg]] [[file:images/cooperative-scheduling.jpg]]
* 18. Preemptive Scheduling * 17. Preemptive Scheduling
[[file:images/preemptive-scheduling.jpg]] [[file:images/preemptive-scheduling.jpg]]
* 19. Preemptive Scheduling * 18. Preemptive Scheduling
[[file:images/preemptive-scheduling-tree.jpg]] [[file:images/preemptive-scheduling-tree.jpg]]
* 20. Multi Core * 19. Multi Core
[[file:images/preemptive-scheduling-multi-core.jpg]] [[file:images/preemptive-scheduling-multi-core.jpg]]
* 21. Concurrency versus Parallelism * 20. Concurrency versus Parallelism
- Parallelism :: performing computations at the same time. - Parallelism :: performing computations at the same time.
- Multiple cores. - Multiple cores.
- Multiples computers. - Multiple computers.
- Kind of like having multiple cooks in a kitchen. - Kind of like having multiple cooks in a kitchen.
- Can do different things at the same time. - Can do different things at the same time.
- Can do one job faster (peeling a lot of potatoes). - Can do one job faster (peeling a lot of potatoes).
@@ -142,13 +148,14 @@ Combination of:
- Concurrency :: performing computations over inter-leaving, non-sequential time periods. - Concurrency :: performing computations over inter-leaving, non-sequential time periods.
- Single or multi-core. - Single or multi-core.
* 22. Processes and Threads * 21. Processes and Threads
A little more nuance. A little more nuance.
- Different /recipes/ - Different /recipes/
- Different /parts/ of a recipe - Different /parts/ of a recipe
- Process :: a program in execution. - Process :: a program in execution.
- Unique resources (memory, code, files, etc.).
- Start off with one thread. - Start off with one thread.
- Can /fork/. - Can /fork/.
- Communicate via inter-process communication (IPC). - Communicate via inter-process communication (IPC).
@@ -157,17 +164,17 @@ A little more nuance.
- Share resources ∴ less resource intensive. - Share resources ∴ less resource intensive.
- Can communicate via shared resources. - Can communicate via shared resources.
* 23. Processes and Threads * 22. Processes and Threads
[[file:images/process-vs-thread.jpg]] [[file:images/process-vs-thread.jpg]]
Source: https://stackoverflow.com/questions/16354460/forking-vs-threading/16354658 Source: https://stackoverflow.com/questions/16354460/forking-vs-threading/16354658
* 24. htop * 23. htop
Press ~H~ to show threads. Press ~H~ to show threads.
* 25. Languages and Runtimes * 24. Languages and Runtimes
- ~C~, ~C++~, ~Rust~ allow for fine-grained control of processes and threads. - ~C~, ~C++~, ~Rust~ allow for fine-grained control of processes and threads.
#+begin_src C :output raw #+begin_src C :output raw
@@ -180,7 +187,7 @@ int main() {
} }
#+end_src #+end_src
* 26. JavaScript * 25. JavaScript
- Single-threaded (for application code). - Single-threaded (for application code).
- Runtimes implement concurrency via an event loop. - Runtimes implement concurrency via an event loop.
@@ -188,42 +195,7 @@ int main() {
Source: [[https://medium.com/preezma/node-js-event-loop-architecture-go-deeper-node-core-c96b4cec7aa4]] Source: [[https://medium.com/preezma/node-js-event-loop-architecture-go-deeper-node-core-c96b4cec7aa4]]
* 27. Go * 26. Aside: Event Loop
Runtime multi-threading, multiplexes goroutines onto threads.
[[file:images/go-scheduler.png]]
Source: https://freethreads.net/2019/01/23/go-runtime-scheduler-design-internals/
* 28. Quick Review
- Sequential versus Concurrent Steps
- Compare with recipes.
- Concurrency versus Parallelism
- Parallelism: multiple cooks, simultaneously /doing/.
- Concurrency: one or more cooks, simultaneously /dealing/ with many tasks.
- Operating System Scheduler
- Processes and Threads
- Languages and Runtimes
* 29. Part 2: Using Concurrency
Typical use cases:
- Network I/O
- Requests between clients and servers.
- Requests to a database.
- Requests between microservices.
- Requests to external APIs.
- User interfaces
- Capturing user input
- Rendering output
- Similar, but independent computations.
- Solving problems that are best expressed with concurrency.
* 30. JavaScript
What is the output of the following? Why? What is the output of the following? Why?
#+begin_src javascript #+begin_src javascript
@@ -238,14 +210,49 @@ setTimeout(() => console.log('2'), 0);
console.log('end'); console.log('end');
#+end_src #+end_src
* 31. Go Examples * 27. Go
*Runtime scheduler* manages multiple threads.
[[file:images/go-scheduler.png]]
Source: https://freethreads.net/2019/01/23/go-runtime-scheduler-design-internals/
* 28. Quick Review
- Sequential versus Concurrent Steps
- Compare with recipes.
- Concurrency versus Parallelism
- Parallelism: multiple cooks, simultaneously /doing/.
- Concurrency: one or more cooks, /dealing/ with many tasks.
- Operating System Scheduler
- Processes and Threads
- Languages and Runtimes
* 29. Part 2: Using Concurrency
Typical use cases:
- Network I/O between:
- Clients and servers.
- Services and database(s).
- Microservices.
- Service and external APIs.
- User interfaces
- Capturing user input
- Rendering output
- Similar, but independent computations.
- Solving problems that are best expressed with concurrency.
* 30. Go Examples
1. Shared Memory with Mutex (Mutual Exclusion) 1. Shared Memory with Mutex (Mutual Exclusion)
- Pitfall: synchronization, debugging. - Pitfall: synchronization, debugging.
2. Message Passing (Channels and Goroutines)
- Pitfall: resources usage, complexity.
* 32. Mutex Example 2. Message Passing (Channels and Goroutines)
- Pitfall: resource usage, complexity.
* 31. Mutex Example
Concurrent updates to a bank account balance. Concurrent updates to a bank account balance.
Code: Code:
@@ -253,31 +260,37 @@ Code:
Run: [[file:cmd/mutex/run.sh]] Run: [[file:cmd/mutex/run.sh]]
- Without mutex. - Without mutex.
- With mutex.
* 33. Data Races * 32. Data Races
[[file:images/data-race.jpg]] [[file:images/data-race.jpg]]
* 34. Mutex * 33. Mutex
/Mutual Exclusion/ /Mutual Exclusion/
- Purpose: protect a critical section of code. - Purpose: protect a critical section of code.
- Effect: serialization. - Effect: serialization.
* 35. Race Detector Run: [[file:cmd/mutex/run.sh]]
- With mutex.
* 34. Race Detector
How can we be sure?
Race detector can give us /some/ confidence (but no guarantees).
- Without mutex. - Without mutex.
- With mutex. - With mutex.
* 36. Tradeoffs: Validating Correctness, Testing, Debugging * 35. Tradeoffs: Validating Correctness, Testing, Debugging
- Non-deterministic behavior - Non-deterministic behavior
- Works fine on one run, fatal bug in another. - Works fine on one run, fatal bug in another.
- Up to how the scheduler(s) order things at execution time. - Up to how the scheduler(s) order things at execution time.
- Concealed bugs - Concealed bugs
* 37. Message Passing * 36. Message Passing
Proverbs: Proverbs:
#+begin_quote #+begin_quote
@@ -286,7 +299,7 @@ Don't communicate by sharing memory, share memory by communicating.
Channels orchestrate, mutexes serialize. Channels orchestrate, mutexes serialize.
#+end_quote #+end_quote
* 38. Requirements * 37. Program Requirements
- Read CSV of zip codes - Read CSV of zip codes
- 50 in our example - 50 in our example
@@ -298,27 +311,27 @@ Channels orchestrate, mutexes serialize.
- Display running list of top ten temperatures. - Display running list of top ten temperatures.
* 39. Concurrency Hallmarks * 38. Concurrency Hallmarks
- Repeatedly processing similar data (lines of CSV) - Repeatedly processing similar data (lines of CSV)
- Network I/O - Network I/O
- UI I/O - UI I/O
* 40. Message Passing Example * 39. Message Passing Example
Run: [[file:cmd/csp/run.sh]] Run: [[file:cmd/csp/run.sh]]
* 41. Code Walkthrough * 40. Code Walkthrough
[[file:cmd/csp/main.go]] [[file:cmd/csp/main.go]]
* 42. Channels and Goroutines * 41. Channels and Goroutines
[[file:images/message-passing-example-diagram.jpg]] [[file:images/message-passing-example-diagram.jpg]]
* 43. Channels and Goroutines * 42. Channels and Goroutines
[[file:images/go-scheduler.jpg]] [[file:images/go-scheduler.jpg]]
* 44. Scaling * 43. Scaling
What if: What if:
- We wanted to process the entire list of 42,000 zip codes? - We wanted to process the entire list of 42,000 zip codes?
@@ -326,7 +339,7 @@ What if:
Could we just get rid of the limiter? Could we just get rid of the limiter?
* 45. Resources * 44. Resources
Probably not, because: Probably not, because:
@@ -339,12 +352,12 @@ Probably not, because:
- Better data structures and algorithms could help here. - Better data structures and algorithms could help here.
- Goroutines are cheap, but not free. - Goroutines are cheap, but not free.
* 46. Possible Solutions * 45. Possible Solutions
- Use a semaphore to limit concurrency. - Use a semaphore to limit concurrency.
- Buffers - Buffers
* 47. Designing and Managing Concurrency * 46. Designing and Managing Concurrency
- Size of input matters. - Size of input matters.
- Resources - Resources
@@ -353,7 +366,7 @@ Probably not, because:
- Backpressure - Backpressure
- Complexity - Complexity
* 48. Quick Review * 47. Quick Review
Techniques for implementing and managing concurrency: Techniques for implementing and managing concurrency:
- Event loop - Event loop
@@ -367,7 +380,7 @@ Concurrency Pitfalls:
- Complexity - Complexity
- Need to think differently about concurrent code. - Need to think differently about concurrent code.
* 49. Part 3: Infrastructure Concurrency * 48. Part 3: Infrastructure Concurrency
Concurrent applications running concurrently on distributed computers. Concurrent applications running concurrently on distributed computers.
@@ -376,20 +389,20 @@ Concurrent applications running concurrently on distributed computers.
- Databases - Databases
- Host and Network Infrastructure - Host and Network Infrastructure
* 50. Example * 49. Example
[[file:images/infrastructure-data-race.jpg]] [[file:images/infrastructure-data-race.jpg]]
* 51. Review * 50. Review
- What concurrency is - What concurrency is
- Sequential versus concurrent - Sequential versus concurrent
- Comparison with recipes - Comparison with recipes and cooks
- Compared to parallelism - Compared to parallelism
- How it is implemented - How it is implemented
- Operating System Scheduler and Interrupts - Operating System Scheduler and Interrupts
- Languages and Runtimes - Languages and Runtimes
* 52. Review * 51. Review
- When, why, and how to use it - When, why, and how to use it
- I/O + UI - I/O + UI
@@ -398,16 +411,16 @@ Concurrent applications running concurrently on distributed computers.
- Pitfalls - Pitfalls
- Synchronization - Synchronization
- Resources Usage - Resource Usage
- Debugging - Debugging
- Complexity - Complexity
* 53. Review * 52. Review
- Infrastructure - Infrastructure
- Data Race - Data Race
* 54. Conclusion * 53. Conclusion
- Goals: - Goals:
- Give you a rough sense of what concurrency is. - Give you a rough sense of what concurrency is.
@@ -421,7 +434,7 @@ Concurrent applications running concurrently on distributed computers.
- Tradeoffs and economics (resources, costs, developer time). - Tradeoffs and economics (resources, costs, developer time).
- Learning is continuous... - Learning is continuous...
* 55. Further Reading * 54. Further Reading
- [[https://slikts.github.io/concurrency-glossary/][Concurrency Glossary]] - [[https://slikts.github.io/concurrency-glossary/][Concurrency Glossary]]
- [[https://blog.golang.org/waza-talk][Concurrency is not parallelism]] - [[https://blog.golang.org/waza-talk][Concurrency is not parallelism]]