#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t mutex;
void setup() {
Serial.begin(9600);
xTaskCreate(task1, "print task", 128, NULL, 2, NULL );
xTaskCreate(task2, "print task", 128, NULL, 2, NULL );
mutex = xSemaphoreCreateMutex();
}
void loop()
{
// Empty. Things are done in Tasks.
}
void task1(void *pvParameters)
{
while(1){
if (xSemaphoreTake(mutex, 10) == pdTRUE){
Serial.println("hello from task 1");
xSemaphoreGive(mutex);
taskYIELD();
}
}
}
void task2(void *pvParameters)
{
while(1){
if (xSemaphoreTake(mutex, 10) == pdTRUE){
Serial.println("hello from task 2");
xSemaphoreGive(mutex);
taskYIELD();
}
}
}