Combine FreeRTOS tasks and C++ objects

mourad7 wrote on Wednesday, February 22, 2017:

Hello,
Actually,i try to use FreeRTOS and C++ language inside my project, but i don’t know what is the best way to do this especially when i want to create and call objects ! is it by :

  • Creating object from within a task ? or
  • Create a task from within an object ( i think wrappers are needed in this case) or
  • Combine the task and boject ( 1 task correspnds to on object ) and the deletion of the one means the deletion of the other.

Thank you

heinbali01 wrote on Thursday, February 23, 2017:

Hi Mourad,

Creating object from within a task ?

Objects ( instances of C++ classes ) can be created and used from within a task. In fact, after vTaskStartScheduler() has been called, all code is running from within a task. Except ISR’s, who have their own life and rules.

Create a task from within an object ( i think wrappers are needed in this case)

That is also possible. Wrappers will make your code nicer, easier to read, but they’re not necessary.

When creating tasks ( from within an object, or wherever ), remember that you work on an embedded project, with limited resources: limited in CPU-speed, limited in memory.

When you start a modern browser on a laptop, it will easily create 7 processes, each process will create 16 threads, and the program will allocate e.g. 37 MB of RAM.

On the contrary, an embedded HTTP-server ( from FreeRTOS+TCP ) will run in a single task and allocate KB’s in stead of MB’s. Well, unless your hardware has external SDRAM.

Combine the task and boject ( 1 task correspnds to on object )
and the deletion of the one means the deletion of the other.

That’s a nice idea: each task (except Idle, that you do not own) is bound to an object, and when the object is deleted, its task will be terminated.
In another thread about C++, yesterday, I referred to Richard Damon’s C++ project
I wrote that new() and delete() must be redefined in order to use task-safe allocators. These allocators are defined in one of the heap_x.c modules.

I often like the addition of C++ to embedded projects. The presence of constructors and destructors formalise important steps. Object-oriented thinking can make life easier.

mourad7 wrote on Thursday, February 23, 2017:

Hi Hein,

Thank you for the explaination.I think that creating an object from within a task could be the best choice for my project.

As you said, i have a limited resources especially in term of RAM, which makes the creation of a task from within an object or linking a task to an object a little bit difficult ( knowing that i have a lot of objects to create particulary at the beginig of the program which demand a lot of memory ) aren’t the best choices!

Thank you very much for your help

richarddamon wrote on Thursday, February 23, 2017:

Putting your tasks into objects can be helpful to keep things organized. Certainly most objects will not have a task as part of them (have a reason to create a task). It is also quite possible to use C++ and virtually no heap (or no post-initilization heap), though it does require some discipline and knowledge of what things you need to avoid.