Skip to content

Latest commit

 

History

History
85 lines (48 loc) · 3.4 KB

3_ConditionVariablesVsMutex.md

File metadata and controls

85 lines (48 loc) · 3.4 KB

Detailed Notes on Condition Variables and Mutexes 📝

Overview 🌐

Condition variables allow threads to have finer control over when to block or resume. They are particularly useful for coordinating access to shared resources in a multi-threaded program. Mutexes are another mechanism for controlling access but are more limited in their scope.

Key Concepts 🔑

Competing Threads 👥

  • A set of threads competing for a shared resource in a program.
  • Condition variables control which competing thread to block or resume.

Consumer Thread Example 🛒

  1. Thread T1 is a consumer that wants to consume an element from a queue.
  2. If the queue is empty, T1 will block itself.
  3. T1 will resume when another thread adds an element to the queue.

Condition-based Locking 🔐

  • Condition variables enable threads to block or wake up when certain conditions are met, unlike Mutexes which only handle access control.

Mutex and Condition Variables Difference 🤔

  • Mutex grants access if the resource is not already locked.
  • Condition variables allow a thread to block based on the state of a resource, offering more control.

Real-world Example 🌎

Laptop Access 💻

  • Mutex: Grants access to a laptop if it is not being used by someone else.
  • Condition Variable: Grants access to a laptop only if it is not being used by someone else and has an internet connection.

Code Structure 🖥️

  • With mutexes, once a thread gains a lock, it can perform any operation on the resource.
  • In condition variables, a thread can get an exclusive access to the resource and check additional conditions before proceeding.

In Summation ✅

  • Condition variables and Mutexes are used in combination.
  • Condition variables are used for coordination, not mutual exclusion.
  • APIs like pthread_condition_wait and pthread_condition_signal are used for blocking and signaling threads, respectively.

Interview Questions and Answers ❓🗂️

Q1: What is the main difference between a Mutex and a Condition Variable? 🤔

A1:

  • Mutex: Used for mutual exclusion to control access to a shared resource.
  • Condition Variable: Used for coordination between threads, allows thread to block itself based on additional conditions.

Q2: Can Condition Variables be used without Mutexes? 🧐

A2:

  • No, condition variables are often used in conjunction with mutexes. The mutex helps in ensuring that while a thread is inspecting a resource's state, no other thread will alter it.

Q3: Explain the concept of "Condition-based Locking". 🔒

A3:

  • Condition-based locking refers to the ability of threads to block or wake up based on custom conditions. Unlike mutexes, which only provide binary locked/unlocked states, condition variables provide more nuanced control.

Q4: What is the role of the pthread_condition_wait and pthread_condition_signal APIs? 🛠️

A4:

  • pthread_condition_wait: Used by a thread to block itself if a certain condition is not met.
  • pthread_condition_signal: Used to signal another already blocked thread to resume its execution.

Q5: In the consumer thread example, what would happen if the queue is empty? 📦

A5:

  • If the queue is empty, the consumer thread (T1) would block itself and wait until another thread places an element in the queue.

I hope these notes and interview questions are helpful for your revision! 📚💡