StuBS
Semaphore Class Reference

Semaphore used for synchronization of threads. More...

#include <sync/semaphore.h>

Inheritance diagram for Semaphore:
[legend]
Collaboration diagram for Semaphore:
[legend]

Public Member Functions

 Semaphore (unsigned c=0)
 Constructor; initialized the counter with provided value c.
void p ()
 Wait for access to the critical area.
void v ()
 Leave the critical area.
Public Member Functions inherited from Waitingroom
 Waitingroom ()
 Constructor.
virtual ~Waitingroom ()
 Destructor.
virtual void remove (Thread *customer)
 Remove a given thread prematurely from the Waitingroom.
Public Member Functions inherited from Queue< Thread >
void enqueue (Thread *item)
 Enqueues the provided item at the end of the queue.
Threaddequeue ()
 Removes the first element in the queue and returns it.
Iterator begin ()
Iterator end ()
Threadremove (Thread *item, bool(*cmp)(Thread *, Thread *)=[](Thread *a, Thread *b) {return a==b;})
 Removes and returns a single element from the queue.
void insertFirst (Thread *item)
 Adds item to the beginning of the queue.
void insertAfter (Thread *old_item, Thread *new_item)
 Inserts the element new_item directly after old_item.
Threadfirst ()
 Returns the first element in the queue without removing it.
Threadnext (Thread *o)
 Returns the next element in the queue for a given element.

Detailed Description

Semaphore used for synchronization of threads.

The class Semaphore implements the concept of counting semaphores. The waiting list is provided by the base class Waitingroom.

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore::Semaphore ( unsigned c = 0)
inlineexplicit

Constructor; initialized the counter with provided value c.

Parameters
cInitial counter value
Todo
Implement Constructor

Member Function Documentation

◆ p()

void Semaphore::p ( )

Wait for access to the critical area.

Enter/Wait operation: If the counter is greater than 0, then it is decremented by one. Otherwise the calling thread will be enqueued into the Waitingroom and marked as blocked.

Todo
Implement Method

◆ v()

void Semaphore::v ( )

Leave the critical area.

Leave operation: If there are threads in the Waitingroom, wake the first one; otherwise increment the counter by one.

Todo
Implement Method

The documentation for this class was generated from the following files: