Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Thread-safe C runtime library by supporting POSIX threads

&pagelevel(3)&pagelevel

Programs that work with the POSIX threads described in the XPG5 standard assume that the functions of the runtime system are thread-safe.

To guarantee the thread-safety of the C runtime library, access to global resources (files, global data from the C globals) must be forbidden or protected by a LOCK so that at most one thread can access these resources at a time. The call interface of the functions does not change when this is done. However, a calling thread-1 can be blocked by a thread-2 that has already allocated the resources requested. Only after thread-2 has released the resources can thread-1 access them.

For this reason in CRTE a thread-safe variant is also supplied.

Thread safety in the runtime library is realized using the following mechanisms:

  • exclusive access to objects of type (FILE *)

    All functions that access objects of type (FILE *) behave as if they internally use the flockfile() and funlockfile() functions to obtain exclusive access to these (FILE *) objects.

  • exclusive access to global data anchored in the globals

    Functions that access global data are protected by a LOCK.

  • errno is thread-specific

    errno does not belong to the global data set any more, but is now thread-specific. This means that for every thread of a process, the value of errno is not affected by function calls or the assignment of a value to errno by a different thread.

  • POSIX thread functions

    POSIX thread functions implement exclusive access to objects of type (FILE *).

    The following categories of POSIX thread functions exist:

    • POSIX thread functions that are reentrant (containing an "_r" in the name of the function)

    • POSIX thread functions that are automatically protected by a LOCK

    • POSIX THREAD functions for locking and unlocking objects of type (FILE*)

    • POSIX thread functions for explicitly locking clients

    • POSIX thread functions that affect a process or thread

    The individual POSIX functions are described in detail in the chapter “Functions andvariables in alphabetical order” (see "Functions and variables in alphabetical order").

  • Extended header files

    The following header files contain additional function prototypes, data types and constants to support POSIX threads:

    • <dirent.h>

    • <grp.h>

    • <pthread.h>

    • <pwd.h>

    • <sched.h>

    • <signal.h>

    • <stdio.h>

    • <stdlib.h>

    • <string.h>

    • <time.h>

    • <unistd.h>

Some functions are not thread-safe yet and may not be used in programs that apply multithreading. This fact is pointed out in the description of this function in chapter“Functions and variables in alphabetical order” (see "Functions and variables in alphabetical order").

In addition, the functions of the _POSIX_THREAD_SAVE_FUNCTIONS group were
added. A list of these functions can be found on "Functions to support POSIX threads". These functions are described in detail in the chapter “Functions and variables in alphabetical order” (see "Functions and variables in alphabetical order").