Managing a multithreaded environment

Technical Note 95292






11/6/2015 1:03 PM


While using a multithreaded environment there is a need to use multithread support in the standard library, (for example calls to malloc()).


Multi thread support can be enabled in the standard library. Read the chapter: 'Enabling Multithread Support' in the Development Guide.


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 Freescale MQX [_mem_alloc].
    • 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


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:


This can be done via the IDE option: Project -> Options -> General Options -> Library Configuration -> Enable thread support in the library.

Using versions 6.40 to 6.50

If using versions 6.40 or 6.50 add a number of linker options (--redirect) to Project -> Options -> Linker -> Extra Options to enable multithread support.

The --redirect options are documented in the file


Note that these options need to be added, even when using an RTOS implementation.

See the links for the DLib_Threads.h information, and an example project: Example project (version 6.50.6).zip.


All product names are trademarks or registered trademarks of their respective owners.

We do no longer support Internet Explorer. To get the best experience of, we recommend upgrading to a modern browser such as Chrome or Edge.