Concurrency Flashcards
Do threads use the same address space, or do they copy the same address space? Why?
They use the same address space, so that they do not need to perform a context switch each time we switch threads.
What happens to the stack when we run a multithreaded process?
Each thread will have its own stack.
What are some reasons for using threads?
Parallelism
To avoid blocking programs due to slow IO
It is easy to share data between threads
What are race conditions and critical sections?
A race condition is where the result of an operation may not be deterministic, depending on which thread runs first. Critical sections are areas of code where these race conditions can cause significant problems, and we want to enable mutual exclusion.
What are atomicity violation bugs, and how can we solve them?
This is when instructions get interrupted halfway through a critical section, so that multiple threads modify it. It can be solved with locks.
What is an order violation bug, and how can we solve them?
This is when we intend for one thread to run before another, but they run out of order. We can fix this with conditional variables.
What is a deadlock? Why do they occur (4 reasons)
If one thread holds lock1, but also needs lock2, and another thread holds lock2, but also needs lock1, then neither thread will be able to run, and we have a deadlock.
They occur due to:
- mutual exclusion
- threads holding and waiting with locks
- no preemption (so locks cannot be forcibly removed)
- if we have circular waiting chains