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 likeflock(2)andlockf(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:
- 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 π
- 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*filehandleto 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 offlockfile(). 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
unlocked_stdio(3)β go read that one next, she's the sequel π¦
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
- Copyright 2001, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms