Flashcard Deck · 25 cards · Public

Concurrency & Multithreading - Locks, Semaphores & Deadlocks

Master essential concurrency concepts, from understanding race conditions and deadlocks to implementing robust thread safety with mutexes, semaphores, and advanced lock-free techniques for high-performance multi-core applications.

Cards in this deck

(25 cards)

Preview terms and definitions before starting your study session.

#1
Term
Distinguish between Concurrency and Parallelism.
Definition
Concurrency is about dealing with many things at once (managing multiple tasks over time, potentially interleaved on a single core). Parallelism is about doing many things at once (executing multiple tasks simultaneously on multiple cores or processors). Concurrency is a property of the design, while parallelism is a property of the execution.
#2
Term
What does it mean for code to be "thread-safe"?
Definition
Code is thread-safe if it behaves correctly when executed concurrently by multiple threads. This typically means protecting shared mutable state from race conditions and ensuring data integrity through synchronization mechanisms.
#3
Term
Explain what a "race condition" is.
Definition
A race condition occurs when the correctness of a program depends on the relative timing or interleaving of operations by multiple concurrent threads. It typically arises when multiple threads access and modify shared data without proper synchronization, leading to unpredictable or incorrect results.
#4
Term
What is a "data race," and how does it differ from a "race condition"?
Definition
A data race is a specific type of race condition where two or more threads concurrently access the same memory location, and at least one of the accesses is a write, and there is no synchronization to order these accesses. A data race is a sufficient condition for a race condition, but not all race conditions are data races (e.g., a race condition could involve ordering of events without direct shared memory access conflict).
#5
Term
Define a "critical section" in concurrent programming.
Definition
A critical section is a segment of code that accesses shared resources (data structures, variables, I/O devices) and must not be concurrently executed by more than one thread. It requires mutual exclusion to ensure data integrity.
#6
Term
What is the principle of "mutual exclusion"?
Definition
Mutual exclusion is a property of concurrency control, which states that no two concurrent processes or threads can be in their critical section at the same time. It's a fundamental requirement for preventing race conditions when accessing shared resources.
#7
Term
Explain what a "mutex" is and how it's used.
Definition
A mutex (mutual exclusion lock) is a synchronization primitive that grants exclusive access to a shared resource. Only one thread can acquire the mutex at a time. Threads call mutex.lock() to acquire it and mutex.unlock() to release it. If a mutex is already locked, subsequent lock() calls will block until it's released.
#8
Term
Describe a "counting semaphore" and its primary operations.
Definition
A counting semaphore is a synchronization primitive that controls access to a resource with a limited number of instances. It maintains an internal counter, initialized to the number of available resources. Its primary operations are:
  • wait() (or P()): Decrements the counter. If the counter becomes negative, the thread blocks until signal() is called.
  • signal() (or V()): Increments the counter. If there are blocked threads, one is unblocked.
Showing 8 of 25 cards in this deck.
Study All Now

Related Flashcard Sets

Explore similar study materials created by the community

Browse community decks