That is precisely the same model that I use, on multiple OS’s, and it works well.
That said, I am a newbie to FreeeRTOS, and I am worried about how this C++ model interacts with xTaskCreate
. Does xTaskCreate
have a “create suspended” flag?
Consider the following scenario:
- We create a C++ wrapper for a thread class object that uses xTaskCreate or xTaskCreateStatic.
- When the thread starts to run, as stated by Richard Damon, the static function does a reinterpret_cast<> to convert the void pointer argument to the function to be a pointer to the base class of the C++ thread wrapper.
- The static function then invokes pCThreadObject->runnable, as implied by nasher.
- pCThreadObject->runnable starts immediately doing whatever it wants to do, right-away.
- Some programmer derives a new class from CThreadObject and overrides CThreadObject::runnable.
Now we see a problem:
Upon construction of CThreadObject
, the static running thread will attempt to invoke CDerivedObject::runnable
instead of CThreadObject::runnable
will think that it is OK to operate against the portion of the aggregate object that pertains only to CDerivedObject
. Yikes! Race-condition! The program will likely crash.
The most elegant solution to this problem is to allow xTaskCreate
to specify that the new thread is to be created suspended. The new threaad should not be allowed to run at all until vTaskResume
is invoked. There are other work-arounds that can be done by the programmer, but this is the simplest solution.
Does FreeRTOS allow a thread to be created in a suspended state?