What is a synchronous operation?
Important Differences between Concurrent Programming vs. Distributed Systems
What is concurrency?
How does concurrency occur in a computer system?
Processes, threads, interrupts, hardware
How do we control concurrency?
Python dictionary is not thread safe, why?
\_\_hash\_\_ method in Python has multiple bytecodes, which means its not atomic therefore get and put are not thread-safe in Python, in the sense they don’t cause crashes or garbage values, so you can still have race conditions.What data structure would you use to communicate data between threads?
Use theQueuemodule’s Queue data type as the preferred way to communicate data between threads. Otherwise, use the threading module and its locking primitives. Learn about the proper use of condition variables so you can usethreading.Conditioninstead of using lower-level locks.
What is Thread Safety?
Thread safety means that two threads cannot modify the same object at the same time, thereby leaving the system in an inconsistent state.
Thread Safety?
Thread Safety
What is a race condition?
A race condition occurs when the correctness of a computation depends
on the relative timing or interleaving of multiple threads by the runtime; in other
words, when getting the right answer relies on lucky timing.4 The most common
type of race condition is check-then-act, where a potentially stale observation is
used to make a decision on what to do next.
Race condition example is reading data that is stale and taking action on that read
What is the reason to use concurrency?
Deadlock
A deadlock happens if execution threads mutually claim resources that the other execution threads need (need to meet 4 of the Coffman conditions)
What is a lock?
Its a synchronization primitive that allows us to block out areas in our code so only 1 thread can have access for some critical section in our code, which involves that thread mutating shared state between threads. It is used to avoid race conditions when we have multiple threads that share state and there is logic to mutate that shared state, so locks come in to guard against race conditions, such as a thread reading stale data and taking action on it. Typically we will guard sections in code for both read/writes for this shared mutable state.
Threads can work together. What if a thread reads a result before another thread writes it?
Threads need to synchronize with each other.
A Barrier is a point in the program where threads stop and wait. When all threads have reached the barrier, they can proceed.
Use when you want for a series of threads to hit some point before the overall task can proceed.
We block until we hit the number of threads needed to then pass the barrier, until then we block and wait until we hit the thread count we need for the barrier.