Title: 544
Tags: lec8

Q: What is a "race condition"?
A: A bug, where whether or not you get correct behavior depends on how threads get scheduled.
Race conditions are tricky to test+debug because they're non-deterministic (not same behavior each time), and you might only see the incorrect behavior rarely (say one in a million times).

Q: What is "deadlock"?
A: This is when threads can never make progress, because they each want to acquire a lock that will never be released.
One example: t1 has A and t2 and B; t1 is waiting to additionally acquire B, and t2 is waiting to additionally acquire A.  This will never happen because neither thread will release its first lock until it has acquired the second lock and done some work.

Q: How can a thread (a) get a lock and (b) let go of it?
A: lock.acquire()
lock.release()

Q: How can you rewrite the following code so that the release will happen, even if an exception occurs?
lock.acquire()
# code with possible exception
lock.release()
A: with lock:
    # code with possible exception

Q: What do you call a section of code where certain interleavings with other code would be a problem?
A: critical section

Q: What is "atomicity" in the context of multi-threaded programs?
A: If a thread is changing multiple things together, we never want to see (say from another thread) partial results, with only some changes made.

Q: What is an "invariant"?
A: A rule that applies to the data, often something a human might know from context.  For example, an invariant might be a list of ages cannot contain negative numbers.  Some invariants are known by the system (for example, that the length of the list itself cannot be negative).

Q: What is "consistency" in the context of threading?
A: All invariants are enforced.

Q: If thread t1 holds a lock, what could happen?
1. we could context switch to another thread t2
2. another thread t2 could acquire the lock
A: (1) is possible, (2) is not

Q: What happens in Python when you try to release a lock that has already been released?
A: There is an exception

Q: What is the GIL?
A: Global Interpreter Lock