Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Why mutexes protect shared resources in FreeRTOS - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mutex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do mutexes prevent data corruption?

In FreeRTOS, why does using a mutex help prevent data corruption when multiple tasks access the same variable?

ABecause a mutex automatically fixes corrupted data after tasks finish.
BBecause a mutex speeds up the execution of tasks accessing the variable.
CBecause a mutex duplicates the variable for each task to use separately.
DBecause a mutex allows only one task to access the variable at a time, preventing simultaneous changes.
Attempts:
2 left
💡 Hint

Think about what happens if two friends try to write on the same paper at the same time.

❓ Predict Output
intermediate
2:00remaining
Output of code using mutex to protect shared counter

What will be the output of this FreeRTOS code snippet where two tasks increment a shared counter protected by a mutex?

FreeRTOS
int shared_counter = 0;
SemaphoreHandle_t mutex;

void Task1(void *params) {
  for (int i = 0; i < 5; i++) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    shared_counter++;
    xSemaphoreGive(mutex);
  }
  vTaskDelete(NULL);
}

void Task2(void *params) {
  for (int i = 0; i < 5; i++) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    shared_counter++;
    xSemaphoreGive(mutex);
  }
  vTaskDelete(NULL);
}

// After both tasks finish, what is shared_counter?
ACould be any number between 0 and 10
B10
C0
D5
Attempts:
2 left
💡 Hint

Each task increments 5 times, but the mutex prevents overlap.

❓ Predict Output
advanced
2:00remaining
What error occurs without mutex protection?

Consider two FreeRTOS tasks incrementing a shared variable without a mutex. What error is most likely to happen?

FreeRTOS
int shared_var = 0;

void TaskA(void *params) {
  for (int i = 0; i < 1000; i++) {
    shared_var++;
  }
  vTaskDelete(NULL);
}

void TaskB(void *params) {
  for (int i = 0; i < 1000; i++) {
    shared_var++;
  }
  vTaskDelete(NULL);
}

// What problem can occur here?
ARace condition causing shared_var to have a value less than 2000
BMemory leak due to missing mutex
CDeadlock where both tasks stop running
DCompile error because mutex is missing
Attempts:
2 left
💡 Hint

Think about what happens if two friends try to write the same number on a paper at the same time.

🔧 Debug
advanced
2:00remaining
Identify the bug in mutex usage

What is wrong with this FreeRTOS code snippet that uses a mutex to protect a shared resource?

FreeRTOS
SemaphoreHandle_t mutex;
int shared_data = 0;

void Task(void *params) {
  xSemaphoreTake(mutex, 0);
  shared_data++;
  xSemaphoreGive(mutex);
  vTaskDelete(NULL);
}

// Assume mutex was created before tasks start.
AUsing 0 as block time in xSemaphoreTake causes immediate failure if mutex is not available
BMutex was not created before use
Cshared_data should be volatile to be protected
DxSemaphoreGive should be called before xSemaphoreTake
Attempts:
2 left
💡 Hint

Check the meaning of the block time parameter in xSemaphoreTake.

🚀 Application
expert
2:00remaining
How to avoid priority inversion with mutexes?

In FreeRTOS, when a low priority task holds a mutex needed by a high priority task, what mechanism helps avoid priority inversion?

ADisabling interrupts while the mutex is held
BUsing a binary semaphore instead of a mutex
CPriority inheritance where the low priority task temporarily inherits the high priority
DIncreasing the stack size of the low priority task
Attempts:
2 left
💡 Hint

Think about how the system can help the low priority task finish faster when a high priority task is waiting.

Practice

(1/5)
1. Why do we use mutexes in FreeRTOS when accessing shared resources?
easy
A. To allow only one task to access the resource at a time
B. To speed up the execution of all tasks simultaneously
C. To permanently lock the resource so no task can use it
D. To create multiple copies of the resource for each task

Solution

  1. Step 1: Understand what a shared resource is

    A shared resource is something multiple tasks want to use, like a variable or hardware.
  2. Step 2: Understand mutex purpose in FreeRTOS

    Mutexes ensure only one task uses the resource at a time to avoid conflicts or errors.
  3. Final Answer:

    To allow only one task to access the resource at a time -> Option A
  4. Quick Check:

    Mutex = single task access [OK]
Hint: Mutex means one task at a time [OK]
Common Mistakes:
  • Thinking mutex speeds up all tasks
  • Believing mutex locks resource forever
  • Assuming mutex duplicates resources
2. Which FreeRTOS function is used to lock a mutex before accessing a shared resource?
easy
A. xSemaphoreGive()
B. vTaskDelay()
C. xSemaphoreTake()
D. xQueueSend()

Solution

  1. Step 1: Identify the function to lock a mutex

    In FreeRTOS, xSemaphoreTake() is used to take or lock a mutex.
  2. Step 2: Confirm other functions' purposes

    xSemaphoreGive() releases the mutex, vTaskDelay() delays a task, and xQueueSend() sends data to a queue.
  3. Final Answer:

    xSemaphoreTake() -> Option C
  4. Quick Check:

    Lock mutex = xSemaphoreTake() [OK]
Hint: Take locks, Give unlocks mutex [OK]
Common Mistakes:
  • Confusing xSemaphoreGive() as lock
  • Using vTaskDelay() to lock mutex
  • Mixing queue functions with mutex
3. What will happen if two tasks try to access a shared variable without using a mutex in FreeRTOS?
medium
A. Data corruption or unexpected behavior may occur
B. Both tasks access safely without issues
C. The system automatically locks the variable
D. The second task waits automatically until the first finishes

Solution

  1. Step 1: Understand shared resource risks without mutex

    Without mutex, tasks can change the variable at the same time, causing data corruption.
  2. Step 2: Check FreeRTOS behavior without explicit locking

    FreeRTOS does not automatically lock variables; tasks run concurrently unless synchronized.
  3. Final Answer:

    Data corruption or unexpected behavior may occur -> Option A
  4. Quick Check:

    No mutex = risk of data corruption [OK]
Hint: No mutex means unsafe shared access [OK]
Common Mistakes:
  • Assuming FreeRTOS auto-locks variables
  • Thinking tasks wait automatically
  • Believing no issues happen without mutex
4. Given this code snippet, what is the error related to mutex usage?
if(xSemaphoreTake(mutex, 0) == pdTRUE) {
  sharedVar++;
}
// Missing xSemaphoreGive(mutex) here
medium
A. The mutex is taken twice without giving
B. The mutex is given before taking
C. The shared variable is not incremented
D. The mutex is never released, causing a deadlock

Solution

  1. Step 1: Check mutex take and give usage

    The code takes the mutex but never calls xSemaphoreGive() to release it.
  2. Step 2: Understand consequences of not releasing mutex

    Not releasing the mutex causes other tasks to wait forever, leading to deadlock.
  3. Final Answer:

    The mutex is never released, causing a deadlock -> Option D
  4. Quick Check:

    Missing give = deadlock risk [OK]
Hint: Always give mutex after take [OK]
Common Mistakes:
  • Forgetting to release mutex
  • Assuming increment fails without mutex
  • Confusing order of take/give
5. You have two tasks that both update a shared sensor reading variable. How should you use a mutex to ensure safe updates and avoid blocking tasks too long?
hard
A. Take the mutex once at system start and never release it
B. Take the mutex, update the variable quickly, then give the mutex immediately
C. Update the variable without mutex and rely on task priorities
D. Use two separate mutexes for the same variable

Solution

  1. Step 1: Understand mutex best practice for shared data

    Mutex should be held only briefly to protect the critical section (variable update).
  2. Step 2: Evaluate options for safe and efficient updates

    Taking and giving mutex immediately around update prevents blocking other tasks too long.
  3. Final Answer:

    Take the mutex, update the variable quickly, then give the mutex immediately -> Option B
  4. Quick Check:

    Short mutex hold = safe and efficient [OK]
Hint: Lock only during update, then unlock fast [OK]
Common Mistakes:
  • Holding mutex too long blocking tasks
  • Never releasing mutex causing deadlock
  • Ignoring mutex and risking data errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard