'mutex' was not declared in this scope

I have gone through https://www.freertos.org/a00122.html I am getting error 'mutex' was not declared in this scope. I think I am missing some files that needs to includes

/* Include FreeRTOS APIs and defines */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include <mutex>

SemaphoreHandle_t xSemaphore = NULL;

const int RED = 4;

void setup()
{
   Serial.begin(115200);
   assert(mutex);
   pinMode(RED, OUTPUT);
   
  xTaskCreatePinnedToCore(Task1, "Task1", 1000, NULL, 1, NULL, 0);    
  xTaskCreatePinnedToCore(Task2, "Task2", 1000, NULL, 1, NULL, 0);  
 
}   

void Task1( void * parameter )
{
 
  for(;;)
  {
     xSemaphore = xSemaphoreCreateMutex();
     digitalWrite( RED, HIGH);
     vTaskDelay( 400 );
     digitalWrite( RED, LOW);
     vTaskDelay( 400 );
     xSemaphoreGive(mutex);
  }
}

void Task2( void * parameter )
{
  for(;;)
  {
    if( xSemaphore != NULL )
    {
        /* See if we can obtain the semaphore.  If the semaphore is not
        available wait 10 ticks to see if it becomes free. */
        if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
        {
            /* We were able to obtain the semaphore and can now access the
            shared resource. */

           digitalWrite(RED, HIGH);
            vTaskDelay( 500 );
            digitalWrite(RED, LOW);
            vTaskDelay( 500 );

            /* We have finished accessing the shared resource.  Release the
            semaphore. */
            xSemaphoreGive( xSemaphore );
        }
        else
        {
            /* We could not obtain the semaphore and can therefore not access
            the shared resource safely. */
        }
    }
    
 
  }
}

void loop() {}

How to remove this error ?

Why do you expect a different path and no ".h" for mutex?

This line compile There is no problem I have used it before

mutex.h: No such file or directory

#include <mutex.h>
#include "mutex.h"

get rid of that

SemaphoreHandle_t sema_MQTT_KeepAlive;
SemaphoreHandle_t sema_PublishPM;
SemaphoreHandle_t sema_mqttOK;
SemaphoreHandle_t sema_CollectPressure;

void setup()
{
  sema_PublishPM = xSemaphoreCreateBinary();
  xSemaphoreGive( sema_PublishPM );
  sema_mqttOK    =  xSemaphoreCreateBinary();
  xSemaphoreGive( sema_mqttOK );
  sema_CollectPressure = xSemaphoreCreateBinary();
  xSemaphoreGive( sema_CollectPressure )// change the word binary to mutex

}

this is wrong

for(;;)
  {
     xSemaphore = xSemaphoreCreateMutex();

the creation is done in setup.

1 Like

get rid of that.

1 Like

Thanks This code runs with no error

/* Include FreeRTOS APIs and defines */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"

SemaphoreHandle_t xSemaphore = NULL;

const int RED = 4;

void setup()
{
   Serial.begin(115200);
   xSemaphore = xSemaphoreCreateMutex();
   pinMode(RED, OUTPUT);
   
  xTaskCreatePinnedToCore(Task1, "Task1", 1000, NULL, 1, NULL, 0);    
  xTaskCreatePinnedToCore(Task2, "Task2", 1000, NULL, 1, NULL, 0);  
 
}   

void Task1( void * parameter )
{
 
  for(;;)
  {
     digitalWrite( RED, HIGH);
     vTaskDelay( 200 );
     digitalWrite( RED, LOW);
     vTaskDelay( 200 );
     xSemaphoreGive( xSemaphore);
  }
}

void Task2( void * parameter )
{
  for(;;)
  {
    if( xSemaphore != NULL )
    {
        /* See if we can obtain the semaphore.  If the semaphore is not
        available wait 10 ticks to see if it becomes free. */
        if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
        {
            /* We were able to obtain the semaphore and can now access the
            shared resource. */

           digitalWrite(RED, HIGH);
            vTaskDelay( 800 );
            digitalWrite(RED, LOW);
            vTaskDelay( 800 );

            /* We have finished accessing the shared resource.  Release the
            semaphore. */
            xSemaphoreGive( xSemaphore );
        }
        else
        {
            /* We could not obtain the semaphore and can therefore not access
            the shared resource safely. */
        }
    }
    
 
  }
}

void loop() {}
1 Like

if you use WiFi be very careful about pinning a task to core0. In this case pin one task to core 1 and the other to core 0.

Because headers for STL and other C++ specific libraries don't have a .h. Try it your self by compiling for a Teensy or other ARM-based processor:

#include <mutex>
#include <vector>
#include <memory>
#include <algorithm>

void setup() {
}

void loop() {
}
1 Like

I imagine the ESP32 implementation of std:mutex uses the FreeRTOS functions under the hood. So, this should work if done properly.

Some of this was discussed in OP's previous thread on the topic:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.