Threads and Mutex Flashcards
What is a thread?
A thread is similar to a process: It can execute code but threads are within a process. The main difference is that threads share the same address spaces but each thread has its own stack
Why can the usage of threads be useful?
- Parallel: Work parallel on big data
- I/O: One thread can do I/O while the other is working somehting else
- Periodical: Periodical work can be done by a thread in the background
What is a “critical section”?
A critical section is getting used by multiple threads/processes and is prone to race conditions. It must therefore be protected somehow.
What are 2 ways of dealing with critical sections? Which one is preferable?
- Atomicity: Use HW based atomic operations
- Synchronization primitives: Combination of OS and HW based operations to safe critical section.
Atomicity is too costly, therefore the 2nd.
For what do we use locks? (1 sentence)
To prevent a critical section from being executed by two threads.
How does a mutual exclusion lock (mutex) work?
2 stated:
- locked
- unlocked
What are coarse-grained and fine-grained locking strategies?
- What is the problem with larger ciritcal sections that gets locked?
- coarse-grained: Include more code in the lock as critical section
- fine-grained: Only include the absolute minimum of code as critical section
- Locking large section normally reduces the concurrency (even until no more concurrency is doable)
HW-based lock-support: Disabling Interrupts: Name 2 things why this is a bad idea.
- OS looses control: If an endless loop is in the lock noone can help you.
- Fails for multiple CPUs: Interrupts are per CPU
HW-based lock-support: A simple flag variable
- How does it work?
- What are the problems?
- Uses a variable: If 0 it is free, if 1 it is taken. Method unlock spins until it is free.
- Race conditions
- Spin-waits: Uses CPU time basically for nothing
What is the Test-and-Set Instruction?
The first atomic HW instruction for building locks. Returns old value and sets the pointer to new value.
HW-based lock-support: A valid spin lock
- What is difference to a simple flag variable?
- Does this fix all problems of a simple flag variable?
- It uses an atomic instruction for the unlock function (to be more specific: It uses Test-and-Set)
- It fixes the correctness issue, but it is still not fair (Thread may spin forever and starve).
Explain the HW Operation “Compare-and-Swap”
Atomic operation, Compares a value to an expected value. If it is the same it replaces the value with another value.
Explain the HW Operation “Load-Linked, Store Conditional”
Two instructions work together atomically:
- LoadLinked: Loads a value
- StoreConditional: Only modifies the value if the value wasn’t modified since it was loaded by LoadLinked()
Explain the HW Operation “Fetch and Add”
Atomic operation, takes a value increments it and returns the old value
- How is a “Ticket Lock” implemented?
- Is it better than the other locks mentioned?
- Lock has 2 variables: Turn and ticket (both 0)
- Lock uses fetchAndAdd(ticket).
- Yes because it is correct and because it provides fairness (Ticket system).