Multithreaded I/O

The getc(), getchar(), putc(), and putchar() functions have two versions, one that is defined in the header file, stdio.h, which is a macro and the other which is an actual library routine. The macros have better performance than their respective function versions, but these macros are not thread safe, so in a multithreaded application where _OPEN_THREADS feature test macro is defined, the macro version of these functions are not exposed. Instead, the library functions are used. This is done to ensure thread safety while multiple threads are executing.

The getc_unlocked(), getchar_unlocked(), putc_unlocked(), and putchar_unlocked() functions and macros are functionally equivalent to the getc(), getchar(), putc(), and putchar() functions and macros. These functions and macros can safely be used in a multi-thread environment if and only if called from a thread that owns the FILE* object, such as after a successful call to flockfile() or ftrylockfile().

Use of the getc_unlocked(), getchar_unlocked(), putc_unlocked(), or putchar_unlocked() functions can have unpredictable behavior when used on a thread that has not locked the file.

It is the application's responsibility to prevent deadlocks or looping. For example, deadlock or looping may occur if a FILE* object is closed, or a thread is terminated, before relinquishing all locked FILE* objects