FBB::Semaphore(3bobcat)

Dijkstra's Semaphore
(libbobcat-dev_4.08.03-x.tar.gz)

2005-2018

NAME

FBB::Semaphore - Implements the Semaphore type designed by Dijkstra

SYNOPSIS

#include <bobcat/semaphore>

Linking option: -lpthread -lbobcat

DESCRIPTION

According to http://en.wikipedia.org/wiki/Semaphore_(programming) a semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a parallel programming or a multi user environment. The Semaphore as a data type was designed around 1962 by Edsger Dijkstra.

A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and, if necessary, wait until a unit of the resource becomes available.

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.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

CONSTRUCTORS

Copy and move constructors are not available.

MEMBER FUNCTIONS

EXAMPLE


    #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();
    }
    

FILES

bobcat/semaphore - defines the class interface

SEE ALSO

bobcat(7)

BUGS

None Reported.

DISTRIBUTION FILES

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).