Mod 2: Sync Problems Patterns Flashcards
What are the most common issues with synchronization?
(1) Race Condition
(2) Deadlock
(3) Livelock
(4) Starvation
How can synchronization be done?
(1) Mutual Exclusion
(2) Condition Synchornization
When does a race condition typically happens?
When threads get simultaneous access to update a shared variable
What can a race condition cause?
Unpredictable values of shared variables, as a direct result of the unpredictability of the order in which the threads execute
What can we solve race conditions?
Execute the critical section in an atomic operation to ensure thread safety
What is a race condition?
They occur when two computer program processes, or threads, attempt to access the same resource at the same time and cause problems in the system
What is “starvation”?
When an action in a concurrent programming situation never get executed, i.e when a thread is unable to gain access to a shared resource
When does “starvation” occur?
When shared resources are made unavailable for long period of time by “greedy” threads
How do we avoid starvation?
By letting every thread get a fair amount of time, something that is usually a task for the OS scheduler and not something we neccessarily can affect
What is “deadlock”?
A situation when two or more threads are blocked forever, waiting for each other. In deadlock the threads are in a waiting-state.
What is “livelock”?
Livelock occurs when two or more processes continually repeat the same interaction in response to changes in the other processes without doing any useful work.
eg. threadA is waiting for threadB, and threadB are waiting for threadA
What is the difference between livelock and deadlock?
Thread in deadlock are in a waiting-state and threads in livelock are in a running-state
What is a “barrier”?
Synchronization points that processes/threads must reach before any of them are allowed to proceed
What does a safety property mean?
That each process/thread should never enter a bad state, i e. state in which some variables have faulty values, to avoid corrupt shared data
What does a liveness property mean?
That each process/thread should have liveness property, i e. process/thread eventually enter a good state, in which variables have correct values