ftrylockfile(3)

ftrylockfile3

Stdio locking functions

Part of flockfile(3).

Usage

#include <stdio.h>

void

flockfile(FILE *stream);

int

ftrylockfile(FILE *stream);

void

funlockfile(FILE *stream);

Library

Standard C Library (libc, -lc)

Overview

These functions provide explicit application-level locking of stdio streams. They can be used to avoid output from multiple threads being interspersed, input being dispersed among multiple readers, and to avoid the overhead of locking the stream for each operation.

The flockfile() function acquires an exclusive lock on the specified stream. If another thread has already locked the stream, flockfile() will block until the lock is released.

The ftrylockfile() function is a non-blocking version of flockfile(); if the lock cannot be acquired immediately, ftrylockfile() returns non-zero instead of blocking.

The funlockfile() function releases the lock on a stream acquired by an earlier call to flockfile() or ftrylockfile().

These functions behave as if there is a lock count associated with each stream. Each time flockfile() is called on the stream, the count is incremented, and each time funlockfile() is called on the stream, the count is decremented. The lock is only actually released when the count reaches zero.

Return values

The flockfile() and funlockfile() functions return no value.

The ftrylockfile() function returns zero if the stream was successfully locked, non-zero otherwise.

Releases

55 releases since FreeBSD 5.1-RELEASE; the text changed 32 times.

  1. 5.1–6.3
  2. 6.4
  3. 7.0
  4. 7.1
  5. 7.2
  6. 7.3
  7. 7.4
  8. 8.0
  9. 8.1
  10. 8.2
  11. 8.3
  12. 8.4
  13. 9.0
  14. 9.1
  15. 9.2
  16. 9.3
  17. 10.0
  18. 10.1
  19. 10.2
  20. 10.3
  21. 10.4
  22. 11.0
  23. 11.1
  24. 11.2
  25. 11.3
  26. 11.4
  27. 12.0
  28. 12.1
  29. 12.2
  30. 12.3
  31. 12.4
  32. 13.0–13.2
  33. 13.3–16.0

Standards

The flockfile(), ftrylockfile() and funlockfile() functions conform to IEEE Std 1003.1-2001 (“POSIX.1”).

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.