Mutex Method Design Pattern

In embedded systems programming, a mutex (short for mutual exclusion) is often used to ensure that only one task or thread can access a shared resource at a time. This is crucial to prevent data corruption and other synchronization issues.

Let’s see an example:

C




#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
 
// Define a simple structure to represent the mutex
typedef struct {
    bool locked;
    pthread_mutex_t mutex;
} Mutex;
 
// Function to initialize the mutex
void initMutex(Mutex *mutex) {
    mutex->locked = false;
    pthread_mutex_init(&mutex->mutex, NULL);
}
 
// Function to acquire the mutex
void lockMutex(Mutex *mutex) {
    pthread_mutex_lock(&mutex->mutex);
    while (mutex->locked) {
        // If the mutex is already locked, wait until it becomes available
        pthread_mutex_unlock(&mutex->mutex);
        pthread_mutex_lock(&mutex->mutex);
    }
    // Mark the mutex as locked
    mutex->locked = true;
    pthread_mutex_unlock(&mutex->mutex);
}
 
// Function to release the mutex
void unlockMutex(Mutex *mutex) {
    pthread_mutex_lock(&mutex->mutex);
    // Mark the mutex as unlocked
    mutex->locked = false;
    pthread_mutex_unlock(&mutex->mutex);
}
 
// Example usage of the mutex
Mutex myMutex;
 
void* threadFunction(void* arg) {
    int threadID = *((int*)arg);
 
    lockMutex(&myMutex);
    printf("Thread %d has acquired the mutex.\n", threadID);
    // Critical section - Access the shared resource
    printf("Thread %d is in the critical section.\n", threadID);
    unlockMutex(&myMutex);
 
    pthread_exit(NULL);
}
 
int main() {
    initMutex(&myMutex);
 
    pthread_t thread1, thread2;
 
    int id1 = 1, id2 = 2;
 
    pthread_create(&thread1, NULL, threadFunction, (void*)&id1);
    pthread_create(&thread2, NULL, threadFunction, (void*)&id2);
 
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
 
    pthread_mutex_destroy(&myMutex.mutex);
 
    return 0;
}


Explanation of the above Code:

In this example,

  • A simple mutex using a binary semaphore. The lockMutex function is responsible for acquiring the mutex, and the unlockMutex function releases it.
  • The critical section between these two calls is protected, ensuring that only one thread can access it at a time.
  • The pthread library is used for thread management. Make sure to link your program with the pthread library when compiling (-lpthread flag).

Design Patterns for Embedded Systems in C

When working with embedded systems in C, there are so many design patterns that are particularly very useful. Many design patterns can be applied to embedded systems development in C. In this article, we will discuss design patterns for Embedded Systems in C, let’s see all of them one by one with the help of examples.

Important Topics for Design Patterns for Embedded Systems in C

  • What is a Design Pattern?
  • Creational Design Patterns for Embedded Systems in C
    • Factory Method Design Pattern
    • Object Method Design Pattern
    • Opaque Method Design Pattern
    • Singleton Method Design Pattern
  • Structural Design Patterns for Embedded Systems in C
    • Callback Method Design Patterns
    • Inheritance Method Design Pattern
    • Virtual API Method Design Pattern
  • Other Design Patterns for Embedded System in C
    • Bridge Method Design Pattern
    • Concurrency Method Design Pattern
    • Spinlock Method Design Pattern
    • Mutex Method Design Pattern
    • Conditional Method Design Pattern
    • Behavioral Method Design Pattern

Similar Reads

1. What is a Design Pattern?

Design patterns are defined as the general reusable solutions to the common problems that occur during software development and software designing....

2. Creational Design Pattern for Embedded Systems in C

In embedded systems development in C, creational design patterns are used to abstract the instantiation process of objects, providing flexibility in creating and configuring objects....

2.1 Factory Method Design Pattern

One commonly used creational design pattern is the Factory Method Pattern. Let’s see an example of how we can implement the Factory Method Pattern in C for an embedded system:...

2.2 Object Method Design Pattern

...

2.3 Opaque Method Design Pattern

In the embedded systems programming in C, the Object Pattern is typically implemented using structures and functions that operate on those structures. While C does not have native support for object-oriented programming....

2.4 Singleton Method Design Pattern

...

3. Structural Design Patterns for Embedded Systems in C

In the embedded systems programming, an opaque pattern is often used to hide the implementation details of a data structure or module from the outside world. This helps in encapsulating the internal details, providing a clean interface, and enhancing code maintainability....

3.1 Callback Method Design Patterns

...

3.2 Inheritance Method Design Pattern

...

3.3 Virtual API Method Design Pattern

The Singleton Pattern is basically a type of design pattern that restricts the instantiation of a class to a single instance and provides a global point of access to that instance. This pattern is often used to control access to resources such as databases, file systems, or network connections....

4. Other Design Patterns for Embedded System in C

...

4.1 Bridge Method Design Pattern

Structural design patterns in embedded systems help to organize and structure the code in a way that makes it more modular, flexible, and easier to maintain....

4.2 Concurrency Method Design Pattern

The callback pattern in embedded systems is a design pattern where a function (callback function) is registered to be called later when a specific event or condition occurs....

4.3 Spinlock Method Design Pattern

...

4.4 Mutex Method Design Pattern

In embedded systems programming with C, inheritance is not directly supported as it is in some object-oriented languages like C++....

4.5 Conditional Method Design Pattern

...

4.6 Behavioral Method Design Pattern

The Virtual API design pattern, sometimes called the Virtual Function Table (VFT) pattern, is a valuable technique in embedded C programming for providing a common interface to different implementations of the same functionality....

Conclusion

...

Contact Us