Managing a multithreaded environment
Technical Note 95292
Architectures:
Arm
Component:
compiler
Updated:
9/26/2022 11:27 AM
Introduction
While using a multithreaded environment there is a need to use multithread support in the standard library, (for example calls to malloc()
).
Solution
Multi thread support can be enabled in the standard library. Read the chapter: 'Enabling Multithread Support' in the Development Guide.
Verify
After implementing the system/file lock interface, it is possible to verify that the system lock functions are called when calling for example malloc/free. Just set breakpoints in the *Mtxinit
, *Mtxlock
and *Mtxunlock
functions, and step over a call to malloc/free.
Using an RTOS
If using an RTOS, the first thing to consider is if there is a real reason to implement the multithread support.
Many RTOS vendors have already done that kind of implementation, for example:
- Micrium uC/OS-II and III: See uC/OS-III User's Manual, chapter "Thread Safety of the Compiler’s Run-Time Library".
- Segger embOS: See embOS CPU & Compiler specifics, chapter "Thread-safe system libraries".
- Express Logic's ThreadX: Note that you need to define the symbol TX_ENABLE_IAR_LIBRARY_SUPPORT in both ThreadX and your application.
- Also note that many RTOS:es recommends to use their own heap: for example Express Logic ThreadX [tx_byte/block_allocate]
Using an RTOS and C++
When using the combination of a multi-thread RTOS and C++ the compiler option
--guard_calls
needs to be used. In the IDE, the option can be specified in Project > Options > C/C++ Compiler > Extra Options.
The reason is that C++ constructors/destructors could be called from several RTOS threads.
Using IAR Embedded Workbench for ARM 6.60 and newer
If using IAR Embedded Workbench for ARM version 6.60 and newer enable the linker option:
--threaded_lib
This can be done via the IDE option: Project > Options > General Options > Library Configuration > Enable thread support in the library.
Example project: Example project (version 9.30.1).zip.
All product names are trademarks or registered trademarks of their respective owners.