flockfile(3)

✨ flockfile β€” bestie edition ✨

okay so you know how stdio just works and you never think about it? yeah. this is the page about the little lock gremlin making that happen, and the three functions that let you drive it yourself πŸ’…

the lineup: flockfile, ftrylockfile, funlockfile β€” lock a FILE for stdio. that's the whole vibe.


πŸ“š LIBRARY

Standard C library β€” that's libc, as in -lc. she's everywhere, she's iconic, no cap.


🌸 SYNOPSIS

#include <stdio.h>

void flockfile(FILE *filehandle);
int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);

🧚 Feature Test Macro Requirements for glibc (see feature_test_macros(7))

all three functions up above, yes all of them:

/* Since glibc 2.24: */ _POSIX_C_SOURCE >= 199309L
    || /* glibc <= 2.23: */ _POSIX_C_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

like, define the right macro or these literally won't show up. pick based on your glibc era, iykyk πŸ•°οΈ


πŸ’– DESCRIPTION

so here's the tea πŸ‘‘

the stdio functions are thread-safe. not "mostly," not "if you're lucky" β€” actually thread-safe. how?? each FILE object gets handed a lockcount, and if that lockcount is nonzero, she also remembers her owning thread. then, for every single library call, the functions wait until the FILE object isn't locked by some different thread anymore, lock it, do the I/O you asked for, and unlock it again. every time. automatic. lowkey beautiful.

ok wait, important bestie note: this locking has nothing to do with the file locking done by functions like flock(2) and lockf(3). completely different thing. do not mix them up, i'm begging πŸ™

all of this is invisible to the C programmer. you just write your code and the locking happens behind the scenes. but there are two reasons you might want more detailed control:

  1. maybe a whole series of I/O actions by one thread belongs together, and shouldn't get interrupted by some other thread's I/O barging in πŸ’€
  2. maybe you want to skip the locking overhead entirely for greater efficiency 🏎️

so to handle both of those, a thread can explicitly lock the FILE object, do its little series of I/O actions, then unlock. this keeps other threads from sliding in between. and if you did all this specifically for the efficiency, you do the I/O with the nonlocking versions of the stdio functions β€” that means getc_unlocked(3) and putc_unlocked(3) instead of getc(3) and putc(3). periodt.

okay now the trio, one by one:

  • flockfile() β€” waits for *filehandle to be no longer locked by a different thread, then makes the current thread the owner of *filehandle, and increments the lockcount. she's patient. she waits her turn. 🫢
  • funlockfile() β€” decrements the lock count. that's it. that's the whole job.
  • ftrylockfile() β€” the nonblocking version of flockfile(). she does nothing if some other thread owns *filehandle. otherwise she obtains ownership and increments the lockcount. no waiting, no drama, she just checks and ✨decides✨.

πŸŽ€ RETURN VALUE

ftrylockfile() returns zero for success (the lock was obtained πŸ’…) and nonzero for failure. that's your whole answer right there.


πŸ™ˆ ERRORS

None. literally none. nothing goes wrong here, it's giving flawless ✨


πŸ‘‘ ATTRIBUTES

for an explanation of the terms used in this section, see attributes(7). here's the breakdown:

  • flockfile(), ftrylockfile(), funlockfile() β€” Thread safety: MT-Safe πŸ”₯

(the original puts this in a little table; consider this the same info, just cuter. all three of them, same row, MT-Safe, all day.)


⭐ STANDARDS

POSIX.1-2008. certified, official, on the books πŸ“œ


πŸ•°οΈ HISTORY

POSIX.1-2001.

and fun fact: these functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is defined. so if you're wondering "wait, where are they" β€” that's probably why bestie 😭


🌈 SEE ALSO


that's the whole page!! go lock something (responsibly) πŸ’œ

this was brought to you by the Linux man-pages, version 6.20, dated 2026-02-08 ✨ mwah, stay locked and loaded πŸŽ€

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms