P2L2: Threads and Concurrency Flashcards
What are benefits of multithreading?
- parallelization => speed up - specialization => hot cache - efficiency => lower ram requirement 2 cheaper IPC Single CPU - If the the t_idle > 2*t_ctx_switch then it is useful to use more than 1 thread to avoid wasting too much time in the t_idle If the app is multithreaded and the OS is multithreaded that is helpful because threads can working for each app and OS services
Process vs. thread, describe the distinctions. What happens on a process vs. thread context switch.
- Context switch time in thread is less than in process - Threads usually result in hotter caches when multiple exist compared to thread - Threads can share a virtual address space
When is it useful to add more threads, when does adding threads lead to pure overhead?
..
What are the possible sources of overhead associated with multithreading?
..
What are mutexes?
- It is a construct with infor: locked, owner and blocked threads Functions - lock - unlock - Problem: Two thread manipulate and modify the same data resources at the same time and the result is unpredictable and sometimes wrong - Solution: Mutex uses a lock so that one thread does the work and modified the data resource exclusively. Only then the thread is done the other can continue
What are condition variables?
wait(mutex, cond) signal(cond) Problem: - Having to loop continuously into lock, check the condition and if not unlock. Waste lock time
What are spurious wake-ups, how do you avoid them, and can you always avoid them?
- Thread are awaken from waiting, but they can’t proceed because mutex is still locked. Therefore they are put in the mutex waiting queue - Doesn’t necessarily affect correctness, but affects performance - You can rewrite, put signal and broadcast out of lock if they do not depend on a condition related to the protected data resource and correctness no affected. Else, signal and broadcast need to be in lock
Do you understand the need for using a while() look for the predicate check in the critical section entry code examples in the lessons?
If not used: - It is possible that when the signal is emitted the another thread affected the predicate condition. If not checked then the predicate condition logic could we not followed If used: - The predicate condition logic is always followed because we checked it again to verify it after the signal
What’s a simple way to prevent deadlocks? Why?
- Two or more competing threads are waiting on each other to complete, but none of them ever do - prevent cycle in wait graph - Detection and recovery -Do nothing
Describe the boss-worked multithreading pattern.
- Boss assigns work to workers. Place work in a queue - Worker: performs entire tasks
If you need to improve a performance metric like throughput or response time, what could you do in a boss-worker model? What are the limiting factors in improving performance with this pattern
- Throughput = 1/ boss time per order - Reduce boss time per order - Queue synchronization
Describe the pipelined multithreading pattern.
+ specialization
- balancing and synchronization overheads
If you need to improve a performance metric like throughput or response time, what could you do in a pipelined model? What are the limiting factors in improving performance with this pattern?
- Improve the weakest point in the pipeline by adding more threads
- The limiting factors are maintaining pipeline balance, and additional synchronization operations, weakest point