1. Home
  2. /
  3. Freertos
  4. /
  5. Mutex Example (FreeRTOS)

Mutex Example (FreeRTOS) - Freertos Typing CST Test

Loading…

Mutex Example (FreeRTOS) — Freertos Code

Demonstrates mutual exclusion using FreeRTOS mutex.

#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <stdio.h>

SemaphoreHandle_t xMutex;

void vTask1(void* pvParameters) {
	xSemaphoreTake(xMutex, portMAX_DELAY);
	printf("Task1 in critical section\n");
	xSemaphoreGive(xMutex);
	vTaskDelete(NULL);
}

void vTask2(void* pvParameters) {
	xSemaphoreTake(xMutex, portMAX_DELAY);
	printf("Task2 in critical section\n");
	xSemaphoreGive(xMutex);
	vTaskDelete(NULL);
}

int main() {
	xMutex = xSemaphoreCreateMutex();
	xTaskCreate(vTask1, "T1", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
	xTaskCreate(vTask2, "T2", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
	vTaskStartScheduler();
	return 0;
}

Freertos Language Guide

FreeRTOS is a lightweight, open-source real-time operating system (RTOS) kernel for embedded devices. It provides multitasking, scheduling, and inter-task communication for microcontrollers and small processors.

Primary Use Cases

  • ▸Task scheduling for embedded systems
  • ▸IoT and sensor node software
  • ▸Industrial automation control
  • ▸Real-time motor and sensor management
  • ▸Edge devices and microcontroller applications

Notable Features

  • ▸Preemptive and cooperative multitasking
  • ▸Task priorities and time slicing
  • ▸Inter-task communication (queues, semaphores, mutexes)
  • ▸Software timers and tickless idle mode
  • ▸Memory management and dynamic allocation support

Origin & Creator

Created by Richard Barry in 2003, now maintained and extended by Amazon Web Services as Amazon FreeRTOS and FreeRTOS Kernel.

Industrial Note

Essential in embedded systems requiring predictable real-time behavior, small footprint, and multitasking support-commonly used in IoT sensors, motor control, and safety-related microcontroller applications.

More Freertos Typing Exercises

Hello World Task (FreeRTOS)Blink LED Task (FreeRTOS)Queue Example (FreeRTOS)Timer Callback (FreeRTOS)Semaphore Synchronization (FreeRTOS)Task Delay Example (FreeRTOS)Queue Set Example (FreeRTOS)Priority Task Example (FreeRTOS)Event Group Example (FreeRTOS)

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher