Skip to content

Latest commit

 

History

History
86 lines (58 loc) · 3.23 KB

8_ MutexPosixAPIs.md

File metadata and controls

86 lines (58 loc) · 3.23 KB

Detailed Notes on Mutexes and Data Locking for Interview Preparation 📝

Table of Contents

  1. Introduction to Data Locking
  2. Types of Locking: Code Locking vs Data Locking
  3. Why Code Locking is Inefficient
  4. Understanding Data Locking
  5. POSIX APIs for Mutexes
  6. Best Practices with Mutexes
  7. Interview Questions

Introduction to Data Locking 📚

  • Mutexes are used to protect critical sections of code from concurrent access.
  • Critical sections are portions of code that manipulate shared resources like linked lists.

Types of Locking: Code Locking vs Data Locking 🔒

Code Locking

  • Locking the code segment that's manipulating a data structure.
  • Not efficient for multithreading scenarios.

Data Locking

  • Locking the data structure itself.
  • More efficient as it allows concurrent access to different data structures.

Why Code Locking is Inefficient 🚫

  • Non-conflicting operations on different data structures get serialized, causing inefficiency.
  • Example: A thread deleting from a student list and another deleting from an employee list will block each other unnecessarily.

Understanding Data Locking ✅

  • The Mutex object belongs to the data structure, acting as its bodyguard.
  • Threads trying to manipulate the same data structure will be serialized.
  • But, if two threads are operating on different data structures, they won't block each other.

POSIX APIs for Mutexes 🛠

  • Declare a mutex using pthread_mutex_t
  • Initialize with pthread_mutex_init
  • Lock and unlock using pthread_mutex_lock and pthread_mutex_unlock
  • Destroy the mutex using pthread_mutex_destroy

Best Practices with Mutexes 👌

  • Never copy a data structure containing a Mutex. Doing so results in undefined behavior.

Interview Questions 🤔

Q1: What is the difference between Code Locking and Data Locking?

👉 Answer: Code locking is about protecting a segment of code, which can cause unnecessary serialization of non-conflicting operations. Data locking focuses on the data structure itself, allowing more efficient use of resources.

Q2: Why shouldn't you copy a data structure that contains a Mutex?

👉 Answer: Copying a data structure with a Mutex leads to undefined behavior. Mutexes should be treated as unique entities tied to their original data structures.

Q3: Explain the APIs provided by the POSIX standard for Mutexes.

👉 Answer:

  • pthread_mutex_t: For declaring a mutex
  • pthread_mutex_init: For initializing a mutex
  • pthread_mutex_lock and pthread_mutex_unlock: For locking and unlocking
  • pthread_mutex_destroy: For destroying a mutex

Q4: When should you use Data Locking?

👉 Answer: Data locking should be used in situations where you need to provide concurrent access to different data structures without causing unnecessary blockage or serialization. It's often the preferred method in multithreaded programs.


I hope these detailed notes help you in your interview preparation! Feel free to ask any further questions. 😊