Lecture 9-1
Concurrency
Lecture 9-1 Bart Massey 2015-11-22
What Is Concurrency?
Doing two things "at the same time". Multiple flows of control
Often not actually at the same time:
- Getting the flow of control for a limited time / callbacks
- Changing flow of control via voluntary preemption / coroutining
- Changing flow of control via involuntary preemption / timesharing
Parallelism: Actually doing two things at the same time
Why Concurrency?
Sometimes it can be useful to think about two things as happening independently in the same program
For example, "generate-and-test"
- One piece of code generates random numbers
- The other tests them for primality
Each piece of the program has its own pc, stack and local state
Of course, you may want to take advantage of multiple processors = true parallelism
- Shared memory
Shared channel
Callbacks: Extremely "Poor Person's" Concurrency
Give the testing code a function pointer that it can call to generate the next random number
Give a state argument to the callback so that it can set up its internal state
Generators: Letting Your PL Manage State
Another way: Let the PRNG return a random number and then set it up to "keep going" when the tester is finished.
Each call to the generator will run the PRNG from "where it left off"
Do the same thing with the tester
Threads: Splitting Off Control Completely
Another way: Start the PRNG as a whole separate process, except with the same memory as the generator. You can then use the shared memory to communicate answers.
This is pretty common in a lot of languages / OS now, and can allow pre-emptive multitasking and/or parallelism
It's also extremely error-prone
- Deadlock
- Livelock
- Race Conditions
Thread Synchronization Primitives
Mutex: Blocks other threads from entry when a thread is running in the mutex
Semaphore: Clever blocking counter that is hard to understand
Mailbox: Allows shared message passing
Processes: Splitting Off Everything
Don't have to share memory
Now all communications are through channels
Great isolation, but can be slow and heavyweight
Pipes
Conclusions
Concurrency can be scary
On the other hand, it can simplify a design
Use good judgement; get expert help