Multithreading

Build Configuration

When building newlib, the configuration --enable-newlib-multithread needs to be added to enable multithreading support. It is enabled by default.

_reent Structure

The header file reent.h in Newlib defines the structure struct _reent to support multithreading:

  • By default, Newlib defines a global structure variable impure_data, defined in newlib/libc/reent/impure.c, and represented by the macro _REENT;
  • struct _reent defines some C standard variables, such as errno, stdin, stdout, stderr, etc.;
  • To support multiple threads accessing their own errno, stdin, stdout, stderr, etc., a member of type struct _reent needs to be added to the thread object structure;
  • Due to the large size of struct _reent, XWOS implements struct _reent itself;
  • In addition to providing functions from the C standard, Newlib also defines versions of functions with the _r suffix, such as _read_r(), which have an additional struct _reent * parameter to distinguish between different threads.