Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 3.49 KB

12_SpuriousWakeMultipleConditions.md

File metadata and controls

61 lines (44 loc) · 3.49 KB

How to Avoid Spurious Wake-up in Multi-threading with Multiple Conditions

Overview 📑

This focuses on avoiding spurious wake-ups when dealing with multiple conditions in a multi-threading environment. It builds upon the existing concept that spurious wake-ups can be handled by replacing an if statement with a while condition. It discusses how to effectively deal with multiple conditions through an analogy.

Analogy 🎉

  • Imagine you want to go to a party (a critical section in code).
  • Three conditions must be met:
    1. No Rain: The weather must be clear.
    2. Best Friend in Town: Your best friend should be in town.
    3. Vehicle Availability: A vehicle must be available.

Code Translation 🖥️

  • The conditions are sandwiched between mutex_lock and mutex_unlock.
  • Why? To make sure the conditions are checked in a thread-safe manner.

Scenario Walkthrough 🚶‍♂️

  • If it's raining: You go to sleep (thread is blocked).

    • You can't wake yourself up to check conditions again (thread is blocked, can't inspect).
    • Another thread ("your sibling") wakes you up (sending a signal to blocked thread).
    • You check the condition again. If it's still raining, you go back to sleep.
  • If your friend is not in town: Same process as above, you go back to sleep.

  • If vehicle is not available: Same process as above, you go back to sleep.

Key Flaw 🚨

  • By the time you get to the last condition, the first condition might have changed (e.g., it might start raining again).

Solution 🛠️

  • Every time the thread wakes up, it must recheck all conditions.
  • Use a while statement to combine all the conditions.
  • If conditions are too complicated for one while statement, use logical operators to combine them.

Best Practice 🎯

  • Always check conditions combined within the same while statement to ensure spurious wake-ups don't occur.

Interview Questions and Answers 📝

Q1: What is a spurious wake-up? 🤔

A1:

A spurious wake-up occurs when a thread is awakened from a blocked state even when the condition that it's waiting for hasn't been satisfied. This can lead to unexpected behavior if not properly handled.

Q2: How do you typically avoid a spurious wake-up? 🛑

A2:

The traditional way to avoid spurious wake-ups is to replace an if statement with a while condition loop that checks the condition again upon waking up.

Q3: Why do we need to check all conditions every time the thread wakes up in the context of multiple conditions? 🤷‍♂️

A3:

When dealing with multiple conditions, checking all of them upon wake-up ensures that the thread doesn't proceed with execution unless all conditions are satisfied. This prevents errors and unexpected behavior.

Q4: What does the 'mutex_lock' and 'mutex_unlock' do in this context? 🔒

A4:

mutex_lock and mutex_unlock are used to ensure that condition checks are thread-safe. All conditions to be tested are "sandwiched" between these mutex operations to ensure that no other thread modifies the conditions when one thread is reading them.

Q5: How can you deal with complicated conditions that cannot be easily combined into a single while statement? 🤯

A5:

For more complex conditions, you can use logical operators to combine them. The conditions can also be broken down into separate functions or variables that are evaluated within the while loop. This way, the code remains logically equivalent and still avoids spurious wake-ups.