FLOCKFILE(3)

NAME
flockfile, ftrylockfile, funlockfile - locking for stdio streams

SYNOPSIS
#include <stdio.h>
void
flockfile(FILE *file);
int
ftrylockfile(FILE *file);
void
funlockfile(FILE *file);

DESCRIPTION
These functions provide application control over the locking of stdio streams. flockfile() and ftrylockfile() increment the lock count associated with file on behalf of the calling thread. If another thread owns the lock, flockfile() blocks until the lock becomes available, whereas ftrylockfile() returns immediately and indicates failure.

When called by a thread holding the lock, funlockfile() decrements the lock count by one. When the lock count reaches zero, the calling thread relinquishes ownership of the lock.

Functions such as fread(3) and fprintf(3) are internally thread safe by default, but additional locking may be used to coordinate sequences of multiple calls.

RETURN VALUES
ftrylockfile() returns zero for success and non-zero for failure.

SEE ALSO
getc_unlocked(3), pthreads(3), putc_unlocked(3)

STANDARDS
These functions conform to p1003.1-2024.

HISTORY
These functions have been available since OpenBSD 2.5.

CAVEATS
Reading from one stream can flush a different buffered output stream, leading to deadlocks.