Multithreading
less than a minute
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 innewlib/libc/reent/impure.c, and represented by the macro_REENT; struct _reentdefines some C standard variables, such aserrno,stdin,stdout,stderr, etc.;- To support multiple threads accessing their own
errno,stdin,stdout,stderr, etc., a member of typestruct _reentneeds to be added to the thread object structure; - Due to the large size of
struct _reent, XWOS implementsstruct _reentitself; - In addition to providing functions from the C standard, Newlib also defines versions of functions with the
_rsuffix, such as_read_r(), which have an additionalstruct _reent *parameter to distinguish between different threads.