FBB::Semaphore(3bobcat)
π FBB::Semaphore β from 3bobcat, bestie π
okay so. Bestie. Sit DOWN. We're doing semaphores today and honestly? iconic. This is FBB::Semaphore, a page from section 3bobcat, and it's the whole "Implements the Semaphore type designed by Dijkstra" situation. Yes, that Dijkstra. The 1962 one. We're gonna be so normal about it. π
β¨ NAME β¨
FBB::Semaphore β bestie, this literally implements the Semaphore type, and it was designed by Dijkstra. We stan a legend. π
π¦ SYNOPSIS π¦
#include <bobcat/semaphore>
Linking option: -lpthread -lbobcat
πΈ DESCRIPTION πΈ
So per http://en.wikipedia.org/wiki/Semaphore_(programming), a semaphore is a variable or abstract data type that's used for controlling access, by multiple processes, to a common resource in a parallel programming or a multi user environment. And the Semaphore as a data type? Designed around 1962 by Edsger Dijkstra. Absolute pioneer behavior. π
A useful way to think of a semaphore, bestie, is as a record of how many units of a particular resource are available β coupled with operations to safely (i.e., without race conditions, not even ONE) adjust that record as units are required or become free, and, if necessary, wait until a unit of the resource becomes available. It's giving "the clipboard at the group project," and honestly it ate.
Semaphores are a useful tool in the prevention of race conditions. Semaphores allowing an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores. Both types are supported by Bobcat's implementation. Both!! Inclusive king. π
π NAMESPACE π
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.
π«Ά INHERITS FROM π«Ά
-
(iconic: it inherits from nothing. standalone queen. no cap.)
π CONSTRUCTORS π
Semaphore(size_t nAvailable)β bestie, this constructor defines the semaphore's initial state. With a counting semaphore,nAvailabledefines, e.g., the number of available locations in a storage area. Locking/unlocking, supporting facilities to notify other waiting threads, is implemented via binary semaphores, which are initialized to 1 or 0. A semaphore containing the value 0 blocks. I.e., itswaitmember waits until its value is incremented by another thread, calling one of the semaphore'snotifymembers. π€
Copy and move constructors (and assignment operators) are not available. Sorry bestie, no duplicating this one. π
π MEMBER FUNCTIONS π
void notify()β the internally maintained available value is incremented and one waiting thread (cf. thewaitmembers below) is notified, reactivating that thread. one thread gets picked, and only one. it's giving "tag, you're it." β¨void notify_all()β the internally maintained available value is incremented and all waiting threads (cf. thewaitmembers below) are notified. Only one waiting thread will be able to obtain the semaphore's lock and to reduceavailable, and that particular thread is thereupon reactivated. it's giving "everyone scream, but only one gets the aux." π₯void set(size_t available)β this member blocks until it has obtained the lock of thestd::mutexwhich is maintained internally by theSemaphoreobject. Next theSemaphore'savailablevalue receives the value of the member's argument, and the lock is released. set and release, periodt. πsize_t size() constβ without trying to lock theSemaphoreobject's mutex, the current value of theSemaphore'savailablevalue is returned. lowkey just a peek. no lock, no stress. πvoid wait()β this member blocks for as long as the internally stored value (available) equals zero. When returning fromwaitthe current thread holds the lock of thestd::mutexwhich is maintained internally by theSemaphoreobject. Notify members are used to incrementavailableand to informwaitthat it may return. When multiple threads are waiting only one thread will stop waiting, while the remaining threads will continue to wait for another notification. so like... one of y'all, not all of y'all. πbool wait(Fun fun, Params &&...args)β this is a member template, whereFunis a function (object) receiving the argument passed towait, and returning abool. This member blocks until it has obtained theSemaphore's mutex lock, then, while itsavailablevalue equals 0, waits until being notified. Once it has reacquired the lock after being notifiedfunis called, receivingwait's perfectly forwarded remaining arguments. This member returnsfalseiffunreturnsfalse. It returnstrueiffunreturnstrueandavailablewas unequal zero followingfunreturningtrue. The function may therefore perform tasks outside of theSemaphorelocal environment, which might even involve updating theSemaphore'savailablevalue. she's flexible, she's forward-thinking, she's perfectly forwarding. π§std::cv_status wait_for(std::chrono::duration<Rep, Period> const &relTime)β this member blocks for as long as the internally stored value (available) equals zero and the amount of time specified byrelTimehasn't passed. If the latter happens,std::cv_status::timeoutis returned, otherwisestd::cv_status::no_timeoutis returned, in which case the current thread holds the lock of thestd::mutexwhich is maintained internally by theSemaphoreobject. Notify members are used to incrementavailableand to informwaitthat it may return. When multiple threads are waiting only one thread will stop waiting, while the remaining threads will continue to wait for another notification. clock is ticking bestie β°std::cv_status wait_until(std::chrono::time_point<Clock, Duration> const &absTime)β this member blocks for as long as the internally stored value (available) equals zero and the time specified byabsTimehasn't been reached. If the latter happens (or ifabsTimelies in the past)std::cv_status::timeoutis returned, otherwisestd::cv_status::no_timeoutis returned, in which case the current thread holds the lock of thestd::mutexwhich is maintained internally by theSemaphoreobject. Notify members are used to incrementavailableand to informwaitthat it may return. When multiple threads are waiting only one thread will stop waiting, while the remaining threads will continue to wait for another notification. same energy, but with an absolute deadline. and yes, ifabsTimeis in the past it just times out immediately. π
β EXAMPLE β
okay here's the whole thing, verbatim, don't touch it, it's perfect:
#include <bobcat/semaphore>
using namespace FBB;
Semaphore produce(10); // storage area size
Semaphore consume(0); // # items in store
std::queue itemQueue; // storage queue
void consumer()
{
while (true)
{
consume.wait(); // wait until thereβs an item in store
// mutex lock the queue with multiple consumers
size_t item = itemQueue.front();
itemQueue.pop();
produce.notify(); // notify the producer
process(item); // not implemented
}
}
void producer()
{
size_t item = 0;
while (true)
{
++item;
produce.wait(); // wait for room in the storage
// mutex lock the queue with multiple consumers
itemQueue.push(item);
consume.notify(); // notify the consumer
}
}
int main()
{
thread cons(consumer);
thread prod(producer);
cons.join(); // ending the threads not implemented
prod.join();
}
producer/consumer dance, literally the classic. the way that produce and consume are just vibing with each other. ππ₯
π FILES π
bobcat/semaphore β defines the class interface
π SEE ALSO π
π BUGS π
None Reported.
(we love a clean bill of health. not a single one. slay. β¨)
π«Ά 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'.
πΈ 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 bestie that's the whole page, you're officially a semaphore girlie now π¦ anyway this was libbobcat-dev_6.15.01, dated 2005-2026, and the page is FBB::Semaphore(3bobcat) β go forth and prevent race conditions, mwah πβ¨
π¦ 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