Skip to content

Latest commit

 

History

History
85 lines (51 loc) · 3.7 KB

7_LockingDataLocking.md

File metadata and controls

85 lines (51 loc) · 3.7 KB

Data Locking and Mutexes in Multithreading 🛠🔒

Overview 📝

In multithreading environments, Data Locking plays an essential role in protecting critical sections of code that manipulate shared resources. This concept is usually applied using Mutexes.


Concepts 📚

Function foo() 📖

  • Contains a critical section where operations are performed on a list object (e.g., linked list of students or employees).

Critical Section 🚨

  • A part of code that needs to be protected to prevent concurrent access.
  • Operations can include adding, searching, or deleting a node in the list.

Mutex 🛡

  • Mutexes are used to protect critical sections.
  • In the context of Data Locking, each data structure (like a list) has its own associated mutex, acting like a "bodyguard".

Scenarios 🎭

Scenario 1: Different List Objects 🌐

  • Thread T1 wants to delete a record from a student list.
  • Thread T2 wants to delete a record from an employee list.
  • Both threads act on different list objects, so no critical section exists between them.

Scenario 2: Same List Objects ⚙️

  • Thread T1 and Thread T2 both act on the same student list.
  • Now this becomes a critical section as they act on the same object.

Code Locking vs Data Locking 🤔

Code Locking ❌

  • Involves declaring a global mutex object.
  • All threads lock and unlock this global mutex, causing unnecessary blocking.
  • Not effective when operations are non-conflicting.

Data Locking ✅

  • Each data structure has its mutex object.
  • Threads will only block each other if they act on the same list object.
  • More efficient for non-conflicting operations.

Key Takeaways 🎓

  • Data Locking is generally more efficient than Code Locking.
  • Mutex should belong to the data structure rather than the code.
  • Criticality is dependent not just on the code but also on the objects being acted upon.

Interview Questions 🎤

Q1: Can you explain the concept of a "Critical Section"? 🤔

A1: A "Critical Section" is a part of code that accesses shared resources and thus needs to be protected to prevent concurrent or simultaneous read-write operations which might corrupt the data.

Q2: What is the difference between Code Locking and Data Locking? 🤷‍♂️

A2: Code Locking involves using a global mutex to lock a piece of code, irrespective of which data structures the code is operating on. Data Locking, on the other hand, associates a mutex with each data structure (like a list), ensuring more granular and efficient control over concurrent access.

Q3: When would you use Data Locking over Code Locking? 🧐

A3: Data Locking is generally used when the mutex protection needs to be more specific to the data structures being accessed rather than the code that is executing. It allows for more efficient use of resources and prevents unnecessary blocking of threads.

Q4: How do you decide if a piece of code is a "Critical Section" or not? 👩‍💻

A4: A piece of code becomes a "Critical Section" if it's accessing shared resources that can be concurrently accessed by multiple threads, leading to data corruption or inconsistency. The context in which the code is running, such as which threads are acting on which objects, also plays a role in this determination.

Q5: What are the potential downsides of using Code Locking? 📉

A5: The primary downside is inefficiency. When using Code Locking, you might end up in situations where threads are unnecessarily blocking each other even when they are performing non-conflicting operations on different data structures.


Feel free to study this material and let me know if you have any questions! 🤓✅