Concurrency Method Design Pattern
Concurrency in embedded systems is often crucial to efficiently handle multiple tasks or events concurrently. One common approach is to use a Real-Time Operating System (RTOS) or a simple scheduler for task management.
Concurrency Method Design Pattern is used for Operating System, it is helpfull for single and multiple task, task may be synchronous and asynchronus. Let suppose if there is any user send the request and there is multiple request, so let suppose when there is thread1 goes for task then thread2 have to wait. So waiting period of the other task it takes some time to execute other. In this process we can use the Concurrency Method.
Problem Statement of Concurrency Method Design Pattern
In the above example of a simple concurrency pattern in embedded C using a cooperative multitasking approach without an RTOS. In this example, tasks are executed in a round-robin fashion.
C
#include <stdio.h> #include <stdint.h> // Define task function signatures typedef void (*TaskFunction)( void ); // Define the task structure typedef struct { TaskFunction function; uint32_t interval; // Time between task executions uint32_t lastExecutionTime; } Task; // Example tasks void task1( void ) { printf ("Task 1 executed\n"); } void task2( void ) { printf ("Task 2 executed\n"); } void task3( void ) { printf ("Task 3 executed\n"); } // Array of tasks Task tasks[] = { {task1, 1000, 0}, // Task 1 runs every 1000 milliseconds {task2, 500, 0}, // Task 2 runs every 500 milliseconds {task3, 2000, 0}, // Task 3 runs every 2000 milliseconds }; // Number of tasks #define NUM_TASKS (sizeof(tasks) / sizeof(Task)) // Simple scheduler void scheduler( void ) { // Get the current time (in a real system, this would be a timer value) uint32_t currentTime = 0; // Replace this with actual timer value for ( int i = 0; i < NUM_TASKS; ++i) { Task *currentTask = &tasks[i]; // Check if it's time to execute the task if ((currentTime - currentTask->lastExecutionTime) >= currentTask->interval) { // Execute the task currentTask->function(); // Update the last execution time currentTask->lastExecutionTime = currentTime; } } } int main() { // In a real system, this loop would run indefinitely for ( int i = 0; i < 10; ++i) { // Simulate the passage of time // In a real system, this would be handled by interrupts or a timer // Here, we just increment the time variable for simplicity scheduler(); } return 0; } |
Explanation of the above Code:
- In this example demonstrates a simple scheduler that iterates over an array of tasks, checks if each task should be executed based on its interval, and then executes it if needed.
- The tasks run in a cooperative manner, meaning each task must yield control to the scheduler voluntarily. In a real-world scenario, this could be implemented using timer interrupts or a more sophisticated scheduler in an RTOS.
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
Contact Us