3

so currently i am investigating the possibility in using a pure C++17 Project for an embedded device (Cortex m4). But based on the fact that it is an embedded device we have port and use an RTOS Such as FreeRTOS or uc-OS and i would highly prefer in using the std::thread (So we can easily exchange the RTOS if it is required). Is there a way to override the thread class in C++ to use the platform depended RTOS?

Thanks in advance

Synapsido
  • 140
  • 3
  • 12
  • Your use case is a bit fuzzy. If you have a C++17-compliant compiler for this target and want portability you do not want to rope in a target specific threading primitive. If you do not have a C++17 compiler for this target or any other candidate targets there are no guarantees whatever hackery you use on this target will be applicable to the next. You're likely better off writing an abstraction layer. – user4581301 Jan 27 '20 at 19:52
  • Check out this article: https://www.codeproject.com/Articles/1278513/Cplusplus11-FreeRTOS-GCC. – Arthur Passos Jan 27 '20 at 20:10

2 Answers2

3

Is there a way to override the thread class in C++ to use the platform depended RTOS?

There's no easy way to do this, but there have been attempts to do so by others as pointed out by @Arthur Passo. Even that's not simply overriding the OS specific classes, instead you need to hook toochain calls to look at the FreeRTOS api whenever OS specific call is needed. This would in turn raises so many questions about keeping things maintainable across different compiler versions.

Since I have been doing a similar sort of investigation a few months ago, I reckon the best possible solution would be one of the following. (I would personally stick to option 1 given the amount of flexibility and convenience in maintenance).

  1. Make your own C++ OS abstraction layer on top of CMSIS OS API which most of RTOS providers support(FreeRTOS, KeilRTX, Chibi support it, I am sure uc-OS does it as well). This makes it easier to use a single abstraction with many RTOSes as long as your build system is capable of linking the proper files depending upon the RTOS being used. This at the same time gives you full flexibility to configure thread priorities, stack sizes etc. which might not be possible if you go with something like posix api.

  2. Make your own C++ OS abstraction layer on top of POSIX api. FreeRTOS provides a POSIX API https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/index.html, I'm sure others will have a similar variant of it.

aep
  • 1,583
  • 10
  • 18
  • What about a variant "3." to your propositions: *Make your own C++ OS abstraction layer directly on top of your favourite RTOS library (i.e., leaving out CMSIS-OS or POSIX layers), saving some of the precious resources of your Cortex-M4.* This has the disadvantage that if you want to exchange your RTOS for another, you have to rewrite your C++ OSAL. But hey - you wrote it yourself, and you'll only have to adapt the implementation of your OSAL classes. – HelpingHand Feb 22 '22 at 21:09
0

There is no way to tell std::thread to use use FreeRTOS::thread (made this up) but you can use conditional compilation and a type alias like

#ifdef FREE_RTOS                  // you will need to get the correct symbol from the implementation to check for
using thread_t = FreeRTOS::thread // you will need to use the correct type here
#elif defined(OTHER_RTOS)
using thread_t = OtherRTOS::thread
#else
using thread_t = std::thread
#endif

And n ow thread_t will be the thread type from the implementation and falls back to std::thread if no symbols are found.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • There is a lot more that comes to making FreeRTOS and std::therad compatible than just types. The biggest problem is that these are completly different concepts that differ in usage and thus an abstraction layer is required. – krawacik3 Jan 12 '21 at 08:59