what is a race condition?
A race condition occurs when multiple threads modify shared memory simultaneously, and the outcome depends on order of access.
what is an atomic instruction?
An instruction that is executed as a single, indivisible step. A single machine-code instruction is atomic as it cannot be interuppted midway.
what are critical sections?
Lines of code that need to be protected to prevent a race condition error
what is the purpose of a mutual exclusion lock (mutex)?
Prevents multiple threads from accessing a shared resource at the same time. The thread holding the mutex lock will block any other threads trying to run that code until the mutex is unlocked.
What is a major issue of mutex?
When the unlock call is not executed. This can either happen as a result of a programming mistake or if an exception occurs within the critical section.
what is std::lock_guard?
A class that builds on std::mutex. The lock_guard will lock the mutex then release it when lock_guard goes out of scope. We must ensure the lock_guard goes out of scope as soon as the critical section code is finished.
What is Deadlock?
Deadlock occurs when multiple threads become stuck at a synchronisation barrier, each waiting for resources held by other threads.
What is try_lock()?
One way to prevent a deadlock is using try_lock() to check if mutex is already locked. If it is locked, the call will return false and the thread can release its own resources to allow the other thread to continue, and later attempt try_lock again.
What is a livelock?
-when two or more threads keep changing their states in response to each other.
-However, the the threads keep reacting to each other’s actions in a way that prevents progress. -This is a result of using try_lock().
What is resource starvation?
Starvation happens when a process does not get enough CPU time or resources to make meaningful progress.
what is the purpose of thread_local?
thread_local ensures each thread gets its own independant instance of the variable.
What is a condition variable?
A condition variable enables threads to wait for specific conditions to be satisfied before continuing execution.
what are spurious wakeups?
Spurious wakeups occur when the condition variable wakes threads even if they were not notified.
how are spurious wakeups managed by the condition variable?
the wait() function is given a lambda (inline function) to recheck the condition once the thread wakes up
What is the reason to use unique_lock instead of mutex?
unique_lock enables us to work with condition variables.
what are the steps that a program has when using unique_lock with condition variables?
What is a counting semaphore?
A counting semaphore manages multiple resources by permitting a certain number of threads to access the resource simultaneously. The counter indicates the number of available resources.
What are the two operations of a semaphore?
What are the differences between a mutex and a semaphore?
how do synchronisation barriers work?
a re-entrant function can be invoked again before its previous execution is completed based on what requirements?
When should you write a multi-threaded program?
What is the difference between re-entrant and thread-safe?
-thread-safe function uses shared resources
-but remains safe to be called by multiple threads simultaneously by employing e.g. mutexes.
what is inter-thread communication?
Inter-thread communication occurs between threads within the same process. Since all threads share the same memory space, they can communicate by directly accessing memory.