FLOCKFILE(3) Library Functions Manual FLOCKFILE(3)

flockfile, ftrylockfile, funlockfilelocking for stdio streams

#include <stdio.h>

void
flockfile(FILE *file);

int
ftrylockfile(FILE *file);

void
funlockfile(FILE *file);

These functions provide application control over the locking of stdio streams. () and () 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, () 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.

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

getc_unlocked(3), pthreads(3), putc_unlocked(3)

These functions conform to IEEE Std 1003.1-2024 (“POSIX.1”).

These functions have been available since OpenBSD 2.5.

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

OpenBSD 7.8 July 25, 2025 FLOCKFILE(3)