FBB::Flock(3bobcat)
π FBB::Flock π
okay bestie, welcome to the FBB::Flock page β that's section 3 of the bobcat manual, in case you were wondering where we are π¦. Buckle up, because this one is literally about stopping your programs from stepping on each other's toes.
β¨ NAME β¨
FBB::Flock β allows multiple programs to synchronize access to the information they use π
β¨ SYNOPSIS β¨
#include <bobcat/classname>
Linking option: -lpthread -lbobcat
β¨ DESCRIPTION β¨
so like, imagine a bunch of programs all need the same info β could be files, could be shared memory, whatever. That access has to be synchronized, no cap. Multiple programs reading in parallel? Totally fine, slay. But the second more than one program wants to write/modify that info, it's giving chaos β data corruption era. π
The class FBB::Flock lets programs synchronize reading/writing to files by locking access to a lock file that either already exists or gets created on the spot. Locking comes in two flavours:
- Exclusive β only ONE program can access the info. This is the usual vibe when writing.
- Shared β multiple programs can access the info. This is the usual vibe when reading.
ngl, using Flock's locking facilities is basically the same energy as using mutexes in multi-threaded programs: if programs (or threads of multi-threaded programs) don't use locking facilities, then those programs (or threads) can absolutely break the data's integrity. So yeah β multiple programs touching the same information should be locking. Periodt. π
β¨ NAMESPACE β¨
FBB
Every constructor, member, operator and manipulator mentioned in this man-page is defined in the namespace FBB. iykyk π«Ά
β¨ INHERITS FROM β¨
-
(nothing bestie, it's a free agent π )
β¨ CONSTRUCTORS β¨
Flock()β the default constructor initializes aFlockobject. It can't offer you any synchronization facilities yet because no lock file has been defined. See also the membersetFilenamebelow. πFlock(std::string filename, mode_t mode = 0600)β this constructor initializes aFlockobject usingfilenameas its lock file. Multiple programs can specify the same file name, which is how they get to synchronize their read/write actions. Iffilenamedoesn't already exist, it gets created using access modemode. And listen: merely constructing aFlockobject does not activate locking. To (un)lock, there are member functions for that (see below). β¨
Copy and move constructors (and assignment operators) are not available. Not even a little bit. π
When an FBB::Flock object ceases to exist and the object has acquired a lock, then the FBB::Flock's destructor releases the lock. Cleanup queen. π§
β¨ MEMBER FUNCTIONS β¨
Quick vibe check before we go through them:
- Requesting a shared lock succeeds if no other program has obtained an exclusive lock.
- Requesting an exclusive lock succeeds if no other program has obtained either a shared or an exclusive lock.
Got it? Good. Now the roster πΈ
int fd() constβ returns the file descriptor associated with the current lock file. If no lock file has been specified,-1is returned.std::string const &filename() constβ returns the name of the lock file. If no lock file has been specified, an empty string is returned.void exclusive() constβ this member returns once the program has obtained an exclusive lock. Just sits there patiently being iconic. πbool exclusive(std::chrono::duration<Amount, Resolution> const &time)β returnstruewhen an exclusive lock was obtained withintimetime units, otherwisefalseis returned. So like,exclusive(4s)waits for at most four seconds to obtain an exclusive lock. An exception is thrown if no lock file was specified.void setFilename(std::string const filename)β theFBB::Flockobject usesfilenameas its lock file name. If the object has currently acquired a lock, it is released. Heads up though: this member does not acquire a lock forfilename. πvoid shared() constβ this member returns once the program has obtained a shared lock.bool shared(std::chrono::duration<Amount, Resolution> const &time)β returnstruewhen a shared lock was obtained withintimetime units, otherwisefalseis returned. E.g.shared(4s)waits for at most four seconds to obtain a shared lock. An exception is thrown if no lock file was specified.void unlock() constβ this member releases the lock obtained by theFlockobject. An exception is thrown if no lock file was specified.
β¨ EXAMPLE β¨
here's the whole thing, verbatim, because slay:
#include <iostream>
c
#include <bobcat/flock>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
try
{
if (argc < 3)
{
cerr << "need lockfile name and e or .\n";
return 1;
}
Flock flock{ argv[1] };
cout << "FD = " << flock.fd() << β\nβ;
if (string str; *argv[2] == βeβ)
{
flock.exclusive();
cout << "press Enter... ";
getline(cin, str);
}
else
{
bool ok = flock.exclusive(3s);
cout << "obtained a lock: " << ok << β\nβ;
if (ok)
{
cout << "press Enter... ";
getline(cin, str);
}
}
}
catch (...)
{
cerr << "caught an exception\n";
}
β¨ FILES β¨
bobcat/flockβ defines the class interface π
β¨ SEE ALSO β¨
bobcat(7), flock(2), the std::chrono namespace π
β¨ BUGS β¨
None Reported. (we love a flawless queen π β¨)
β¨ BOBCAT PROJECT FILES β¨
- https://fbb-git.gitlab.io/bobcat/ β gitlab project page π¦
Debian Bobcat project files:
libbobcat6β debian package containing the shared library, changelog and copyright note πlibbobcat-devβ debian package containing the static library, headers, manual pages, and developer info π§
β¨ BOBCAT β¨
Bobcat is an acronym of `Brokkenβs Own Base Classes And Templatesβ. cute and clever, we love that for it π
β¨ COPYRIGHT β¨
This is free software, distributed under the terms of the GNU General Public License (GPL). πΈ
β¨ AUTHOR β¨
Frank B. Brokken (f.b.brokken@rug.nl). π
okay that's the whole page bestie, you're officially Flock-pilled π«Ά this was libbobcat-dev_6.15.01, dated 2005-2026, and it's been giving FBB::Flock(3bobcat) realness the entire time. go lock something (responsibly), ily, bye π
β¨π
π¦ where this page comes from
- ubuntu stonking: libbobcat-dev 6.15.01-1 Β· GPL-3.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms