thread safe „getter“ function with rtos

Logway
Posts: 1
Joined: Tue Jul 31, 2018 12:35 pm

thread safe „getter“ function with rtos

Postby Logway » Tue Jul 31, 2018 1:24 pm

Hi Guys,
I need your help.
How can i make a thread safe „getter“ function with rtos?
Could you help me with this code?

Code: Select all

// file Name: Manager.hpp
#ifndef MANAGER_H_
#define MANAGER_H_

#define CC_ARRAY_WIDTH 2000

#include <Arduino.h>
#include <array>

namespace testExclusive{
  class Manager{
    public:
      Manager();

      std::array<char, CC_ARRAY_WIDTH> getCcarray();
      void              setCcarray(std::array<char, CC_ARRAY_WIDTH> array);
      void              setCcarrayCharAt(int pos, char letter);
      SemaphoreHandle_t &getMutexCcarray();
    private:
      std::array<char, CC_ARRAY_WIDTH> ccArray;
      SemaphoreHandle_t mutexCcarray;
  };
}
#endif

Code: Select all

// file name: Manager.cpp
#include "Manager.hpp"

testExclusive::Manager::Manager(){
  mutexCcarray = xSemaphoreCreateMutex();
  if(mutexCcarray == NULL){
    Serial.println("namespace: testExclusive -> class: Manager -> SemaphoreHandle_t mutexCcarray could not be created");
  }
};

std::array<char, CC_ARRAY_WIDTH> testExclusive::Manager::getCcarray(){
  return ccArray;
};

void testExclusive::Manager::setCcarray(std::array<char, CC_ARRAY_WIDTH> array){
  xSemaphoreTake(mutexCcarray, portMAX_DELAY);
  ccArray = array;
  xSemaphoreGive(mutexCcarray);
};

void testExclusive::Manager::setCcarrayCharAt(int pos, char letter){
  xSemaphoreTake(mutexCcarray, portMAX_DELAY);
  if(pos >= 0 && pos < ccArray.size()){
    ccArray[pos] = letter;
  }
  xSemaphoreGive(mutexCcarray);
};

SemaphoreHandle_t& testExclusive::Manager::getMutexCcarray(){
  return mutexCcarray;
};

Who is online

Users browsing this forum: No registered users and 71 guests