0
0
Power-electronicsConceptBeginner · 3 min read

What is Mutex in Embedded C: Simple Explanation and Example

A mutex in Embedded C is a tool that helps prevent multiple tasks from using the same resource at the same time, avoiding conflicts. It acts like a lock that only one task can hold, ensuring safe access to shared data or hardware.
⚙️

How It Works

Imagine you have one key to a bathroom that many people want to use. A mutex works like that key: only one person can hold it at a time, so only one task can use the shared resource. When a task wants to use the resource, it tries to take the mutex (lock it). If the mutex is already taken, the task waits until it is free.

This prevents problems like two tasks changing the same data at once, which can cause errors or crashes. Once the task finishes using the resource, it releases the mutex (unlocks it), letting others use it safely.

💻

Example

This example shows two tasks trying to access a shared variable. The mutex ensures only one task changes it at a time.

c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_data = 0;

void* task1(void* arg) {
    pthread_mutex_lock(&mutex); // Lock mutex
    shared_data += 1;
    printf("Task 1 updated shared_data to %d\n", shared_data);
    sleep(1); // Simulate work
    pthread_mutex_unlock(&mutex); // Unlock mutex
    return NULL;
}

void* task2(void* arg) {
    pthread_mutex_lock(&mutex); // Lock mutex
    shared_data += 2;
    printf("Task 2 updated shared_data to %d\n", shared_data);
    sleep(1); // Simulate work
    pthread_mutex_unlock(&mutex); // Unlock mutex
    return NULL;
}

int main() {
    pthread_t t1, t2;
    pthread_create(&t1, NULL, task1, NULL);
    pthread_create(&t2, NULL, task2, NULL);
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    printf("Final shared_data value: %d\n", shared_data);
    return 0;
}
Output
Task 1 updated shared_data to 1 Task 2 updated shared_data to 3 Final shared_data value: 3
🎯

When to Use

Use a mutex in embedded systems when multiple tasks or interrupts share resources like variables, memory, or hardware peripherals. Without a mutex, simultaneous access can cause data corruption or unpredictable behavior.

For example, if two tasks try to write to the same sensor or update a shared counter, a mutex ensures only one task does it at a time. This is common in real-time operating systems (RTOS) where multitasking happens.

Key Points

  • A mutex is a locking tool to protect shared resources.
  • Only one task can hold the mutex at a time, preventing conflicts.
  • Tasks must lock the mutex before accessing shared data and unlock it after.
  • Mutexes are essential in multitasking embedded systems to avoid data corruption.

Key Takeaways

A mutex prevents multiple tasks from accessing shared resources simultaneously.
Always lock a mutex before using shared data and unlock it afterward.
Mutexes help avoid data corruption and crashes in multitasking embedded systems.
Use mutexes when tasks or interrupts share variables, memory, or hardware.
Mutex usage is common in real-time operating systems for safe multitasking.