GCGlib  0.04.228
GCG Graphics Engine
gcgPRODUCERCONSUMER Class Reference

Controls and synchronizes multiple producers and consumers accessing a shared and limited buffer. Supports blocking and non-blocking calls to put() and get(). More...

#include <gcg.h>

Inheritance diagram for gcgPRODUCERCONSUMER:
gcgCLASS

Public Member Functions

 gcgPRODUCERCONSUMER (unsigned int bufferlength=4)
 Constructs a valid object with a specified number of buffer elements. Sets put() and get() methods to block if the buffer is full or empty, respectively. If bufferlength is non-zero, calls setBufferSize() to create initial buffer. More...
 
virtual ~gcgPRODUCERCONSUMER ()
 Frees all object resources by calling destroyProducerConsumer()
 
bool setBufferSize (unsigned int bufferlength)
 Frees previous resources and allocates a buffer with different length. Old buffer contents and parameters are completely discarded. More...
 
bool destroyProducerConsumer ()
 Frees all object resources. Invocations to the object methods become innocuous. A call to setBufferSize() turns the object usable again. More...
 
bool put (void *val)
 Puts a new object pointer at the end of the buffer. It's the producer interface. More...
 
void * get ()
 Gets the oldest pointer of the buffer. It's the consumer interface. More...
 
uintptr_t getCounter ()
 Gets the current number of elements stored into the buffer. More...
 
void setTimeout (long timeoutprod, long timeoutcons)
 Sets the blocking policy for producers and consumers when calling put() and get() methods, respectively. When timeout is changed, all waiting threads for production/consumption are awaken. Negatives timeouts make the production/consumption block. With setTimeout(0,0) the production/consumption works changing the internal buffer but put() and get() calls never block. To disable the production/consumption and avoid changes to the buffer by subsequent put() and get() calls, use enable() instead. More...
 
void enable (bool enabled)
 Enables or disables the production and consumption. When enable status is changed, all waiting threads for production/consumption are awaken to check its new value. To avoid blocked calls to put() and get() and have the production/consumption enabled, call setTimeout(0, 0) instead. More...
 
- Public Member Functions inherited from gcgCLASS
void * operator new (size_t size)
 Defines a new operator to be used by instatiations of GCGlib classes instead the global one. More...
 
void * operator new (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by instantiations of GCGlib classes instead the global one. Returns a NULL pointer instead of throwing an exception if an error occurs. More...
 
void * operator new[] (size_t size)
 Defines a new operator to be used by GCGlib array allocations instead the global one. More...
 
void * operator new[] (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by vector allocations instead the global one. More...
 
void operator delete (void *p)
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete[] (void *p)
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 
void operator delete[] (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 

Private Attributes

void * handle
 Internal handle of this object.
 

Detailed Description

Controls and synchronizes multiple producers and consumers accessing a shared and limited buffer. Supports blocking and non-blocking calls to put() and get().

Producer threads and processes call put(void *val) to insert a new and arbitrary object with pointer val in the end of the buffer. Consumer threads and processes call void* get() to obtain the oldest produced object pointer. Synchronized access to the buffer is guaranteed. Calls to put() and get() may (or may not) block the calling thread or process. put() calls block if the buffer is full and get() calls block if the buffer is empty. Use enable() function to enable/disable the production/consumption: the buffer is unchanged by put() and get() calls, which become unblocked and always return with failure. To keep the production/consumption functionality and non-blocking behavior, use setTimeout(0, 0): in this case the buffer can be normally changed by put() and get() calls.

Since
0.01.6

Constructor & Destructor Documentation

◆ gcgPRODUCERCONSUMER()

gcgPRODUCERCONSUMER::gcgPRODUCERCONSUMER ( unsigned int  bufferlength = 4)

Constructs a valid object with a specified number of buffer elements. Sets put() and get() methods to block if the buffer is full or empty, respectively. If bufferlength is non-zero, calls setBufferSize() to create initial buffer.

Parameters
[in]bufferlengthnumber of elements of the producer/consumer buffer. It may be zero.
See also
setBufferSize()

Member Function Documentation

◆ destroyProducerConsumer()

bool gcgPRODUCERCONSUMER::destroyProducerConsumer ( )

Frees all object resources. Invocations to the object methods become innocuous. A call to setBufferSize() turns the object usable again.

Returns
true if the object is completely released.

◆ enable()

void gcgPRODUCERCONSUMER::enable ( bool  enabled)

Enables or disables the production and consumption. When enable status is changed, all waiting threads for production/consumption are awaken to check its new value. To avoid blocked calls to put() and get() and have the production/consumption enabled, call setTimeout(0, 0) instead.

Initially, the gcgPRODUCERCONSUMER object is enabled and ready to requests by put() and get() calls. If enable(false) is called, all threads waiting for production/consumption are awaken and blocked calls of put() and get() return with false/NULL result. All subsequent calls to put() and get() will return unsuccessful immediately. If enabled, the production/consumption normally works with or without blocking as specified by setTimeout() calls.

Parameters
[in]enabledindicates if the production/consumption is to be enabled (true) or disabled (false).
See also
get()
setTimeout()

◆ get()

void* gcgPRODUCERCONSUMER::get ( )

Gets the oldest pointer of the buffer. It's the consumer interface.

Consumer calls to get() will block indefinitely if the buffer is empty, and setTimeout() was called with timeoutcons < 0. It remains blocked until an element is inserted into the buffer by a put() call. If setTimeout() was called with timeoutcons >= 0, a call to get() will try to obtain the oldest pointer during the time specified in microseconds. If successful, it returns the pointer. Otherwise returns NULL.

Returns
the oldest produced object of the buffer if successful or NULL, otherwise.
See also
put()
setTimeout()
enable()

◆ getCounter()

uintptr_t gcgPRODUCERCONSUMER::getCounter ( )

Gets the current number of elements stored into the buffer.

Returns
the number of valid objects stored into the buffer.

◆ put()

bool gcgPRODUCERCONSUMER::put ( void *  val)

Puts a new object pointer at the end of the buffer. It's the producer interface.

Producer calls to put() will block indefinitely if the buffer is full, and setTimeout() was called with timooutprod < 0. It remains blocked until an element of the buffer is consumed by a get() call. If setTimeout() was called with timeoutprod>=0, a call to put() will try to insert the pointer into the buffer during the timeout given in microseconds. If successful, it returns true.

Parameters
[in]valpointer to the produced object to be stored into the buffer.
Returns
true if the object was successfully stored.
See also
get()
setTimeout()
enable()

◆ setBufferSize()

bool gcgPRODUCERCONSUMER::setBufferSize ( unsigned int  bufferlength)

Frees previous resources and allocates a buffer with different length. Old buffer contents and parameters are completely discarded.

Parameters
[in]bufferlengthdefines the number of elements in the producer/consumer buffer. if zero, the object is destroyed by a call to destroyProducerConsumer().
Returns
true if the a buffer with the specified length is successfully allocated.

◆ setTimeout()

void gcgPRODUCERCONSUMER::setTimeout ( long  timeoutprod,
long  timeoutcons 
)

Sets the blocking policy for producers and consumers when calling put() and get() methods, respectively. When timeout is changed, all waiting threads for production/consumption are awaken. Negatives timeouts make the production/consumption block. With setTimeout(0,0) the production/consumption works changing the internal buffer but put() and get() calls never block. To disable the production/consumption and avoid changes to the buffer by subsequent put() and get() calls, use enable() instead.

Parameters
[in]timeoutprodtime to wait in microseconds for production. A negative value means that calls to put() should be indefinitely blocked when the buffer is full. A non-negative value specifies the time in microseconds which put() should wait until the buffer has room for the new product.
[in]timeoutconstime to wait in microseconds for consuming. A negative value means that calls to get() should be indefinitely blocked when the buffer is empty. A non-negative value specifies the time in microseconds which get() should wait until the buffer has a new product.
See also
get()
put()
enable()

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