1 year ago

#373555

test-img

bleuj

FreeRTOS Shared pointer parameter set to 0

I am trying to share data between 2 FreeRTOS tasks. My approach for this is to create a struct for the tasks' pvParameters that contains a std::shared_pointer.

My task creation looks like this:

    auto shared_value = std::make_shared<int8_t>(-1);

    auto paramsA = SharedValParams(shared_value);

    xTaskCreate(taskA,
                "Task A",
                4000,
                (void *)&paramsA ,
                1,
                NULL);

    auto paramsB = SharedValParams(shared_value);

    xTaskCreate(taskB,
                "Task B",
                4000,
                (void *)&paramsB,
                1,
                NULL);

I'm currently trying to modify this value

    void taskA(void *params)
    {
        for (;;)
        {
            *((SharedValParams*)params)->shared_val= 5;
            Serial.printf("Shared val is now %d\n",
                 *(*(SharedValParams*)params).shared_val.get());
            vTaskDelay(1000/ portTICK_PERIOD_MS);
        }
    }

The first couple executions of taskA, shared_val is printed as 5. Great! However, after this shared_val is set to 0, or at least that's what is being printed. I've yet to implement this in taskB, so nothing else accesses or modifies shared_val. I'm unsure as to why this is happening, especially when the default value of shared_val is -1.

In the future once I resolve this issue, I will surround shared_val in a mutex, but for now, I cannot reliably set the value. The motivation behind passing this as a param, is to keep the value scoped between relevant tasks.

c++

shared-ptr

freertos

0 Answers

Your Answer

Accepted video resources