getc_unlocked(), getchar_unlocked(), putc_unlocked(), putchar_unlocked() — Stdio with explicit client locking

Standards

Standards / Extensions C or C++ Dependencies

Single UNIX Specification, Version 3

both  z/OS V1R8

Format

#define _UNIX03_SOURCE 
#include <stdio.h>

int getc_unlocked(FILE *stream);
int getchar_unlocked(void);
int putc_unlocked(int c, FILE *stream);
int putchar_unlocked(int c);

General description

Versions of the functions getc(), getchar(), putc(), and putchar() respectively named getc_unlocked(), getchar_unlocked(), putc_unlocked(), and putchar_unlocked() are functionally equivalent to the original versions, with the exception that they are not thread-safe. These functions may safely be used in a multi-threaded program if and only if they are called while the invoking thread owns the (FILE*) object, as is the case after a successful call to the flockfile() or ftrylockfile() functions.

getc_unlocked, getchar_unlocked, putc_unlocked, putchar_unlocked are provided in a highly efficient macro form. For performance purposes, it is recommended that the macro forms be used rather than the functional forms. By default, stdio.h provides the macro versions of these functions.

However, to get the functional forms, do one or more of the following:
  • Surround the call statement by parentheses, for example, (getc_unlocked)
  • Specify #undef, for example, #undef getc_unlocked
  • For C only: do not include stdio.h.

getc_unlocked, getchar_unlocked, putc_unlocked, putchar_unlocked are not supported for files that are opened with type=record or type=blocked.

getc_unlocked, getchar_unlocked, putc_unlocked, putchar_unlocked have the same restrictions as any read or write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.
Note: Because the macro forms of these functions reevaluate their input arguments more than once, you must not pass an argument that is an expression with side effects.

Returned value

See getc(), getchar() — Read a character and putc(), putchar() — Write a character.

Related information