---
name: "Buffer"
section: "3cc"
platform: "linux"
package: "libcommoncpp2-dev"
version: "1.0.13-5"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/Buffer"
---

# Buffer(3cc)

The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be used to transfer objects between one or more producer and consumer threads. Producer/Consumer buffer for use between threads.

## NAME

Buffer - The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be used to transfer objects between one or more producer and consumer threads. Producer/Consumer buffer for use between threads.

## SYNOPSIS

`#include <thread.h>`

Inherited by **FixedBuffer**.

### Public Methods

**Buffer** (size\_t capacity)\
 *Create a buffer object of known capacity.* virtual **~Buffer** ()\
 *In derived functions, may be used to free the actual memory used to hold buffered data.* size\_t **getSize** (void)\
 *Return the capacity of the buffer as specified at creation.* size\_t **getUsed** (void)\
 *Return the current capacity in use for the buffer.* int **wait** (void \*buf)\
 *Let one or more threads wait for an object to become available in the buffer.* int **post** (void \*buf)\
 *Post an object into the buffer and enable a waiting thread to receive it.* int **peek** (void \*buf)\
 *Peek at the current content (first object) in the buffer.* virtual bool **isValid** (void)\
 *New virtual to test if buffer is a valid object.*

### Protected Methods

virtual int **onPeek** (void \*buf)=0\
 *Invoke derived class buffer peeking method.* virtual int **onWait** (void \*buf)=0\
 *Invoke derived class object request from buffer.* virtual int **onPost** (void \*buf)=0\
 *Invoke derived class posting of object to buffer.*

### Related Functions

(Note that these are not member functions.) int **get** (Buffer &b, void \*o)\
 int **put** (Buffer &b, void \*o)\
 int **peek** (Buffer &b, void \*o)\

## DETAILED DESCRIPTION

The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be used to transfer objects between one or more producer and consumer threads. Producer/Consumer buffer for use between threads.

Producer threads post objects into the buffer, and consumer threads wait for and receive objects from the buffer. Semaphores are used to to block the buffer from overflowing and indicate when there is data available, and mutexes are used to protect multiple consumers and producer threads from stepping over each other.

The buffer class is an abstract class in that the actual data being buffered is not directly specified within the buffer class itself. The buffer class should be used as a base class for a class that actually impliments buffering and which may be aware of the data types actually are being buffered. A template class could be created based on buffer for this purpose. Another possibility is to create a class derived from both **Thread** and Buffer which can be used to implement message passing threads.

**Author:**  David Sugar \<dyfet@ostel.com\>

## CONSTRUCTOR & DESTRUCTOR DOCUMENTATION

### Buffer::Buffer (size\_t capacity)

Create a buffer object of known capacity.

**Parameters:**

- *capacity*

  *is the integer capacity of the buffer.*

### virtual Buffer::~Buffer ()` [inline, virtual]`

*In derived functions, may be used to free the actual memory used to hold buffered data.*

## MEMBER FUNCTION DOCUMENTATION

### size\_t Buffer::getSize (void)` [inline]`

*Return the capacity of the buffer as specified at creation.*

**Returns:**  *size of buffer.*

### size\_t Buffer::getUsed (void)` [inline]`

*Return the current capacity in use for the buffer.*

*Free space is technically* **getSize()** *-* **getUsed()***.*

**Returns:**  *integer used capacity of the buffer.*

**See also:**  **getSize**

### virtual bool Buffer::isValid (void)` [inline, virtual]`

*New virtual to test if buffer is a valid object.*

**Returns:**  *true if object is valid.*

*Reimplemented in* **FixedBuffer***.*

### virtual int Buffer::onPeek (void \* buf)` [protected, pure virtual]`

*Invoke derived class buffer peeking method.*

**Returns:**  *size of object found.*

**Parameters:**

- *buf*

  *pointer to copy contents of head of buffer to.*

*Implemented in* **FixedBuffer***.*

### virtual int Buffer::onPost (void \* buf)` [protected, pure virtual]`

*Invoke derived class posting of object to buffer.*

**Returns:**  *size of object posted.*

**Parameters:**

- *buf*

  *pointer to object being posted to the buffer.*

*Implemented in* **FixedBuffer***.*

### virtual int Buffer::onWait (void \* buf)` [protected, pure virtual]`

*Invoke derived class object request from buffer.*

**Returns:**  *size of object returned.*

**Parameters:**

- *buf*

  *pointer to hold object returned from the buffer.*

*Implemented in* **FixedBuffer***.*

### int Buffer::peek (void \* buf)

*Peek at the current content (first object) in the buffer.*

**Returns:**  *size of object in the buffer.*

**Parameters:**

- *buf*

  *pointer to store object found in the buffer.*

### int Buffer::post (void \* buf)

*Post an object into the buffer and enable a waiting thread to receive it.*

**Returns:**  *size of object posted in bytes.*

**Parameters:**

- *buf*

  *pointer to object to store in the buffer.*

### int Buffer::wait (void \* buf)

*Let one or more threads wait for an object to become available in the buffer.*

*The waiting thread(s) will wait forever if no object is ever placed into the buffer.*

**Returns:**  *size of object passed by buffer in bytes.*

**Parameters:**

- *buf*

  *pointer to store object retrieved from the buffer.*

## FRIENDS AND RELATED FUNCTION DOCUMENTATION

### int get (Buffer & b, void \* o)` [related]`

### int peek (Buffer & b, void \* o)` [related]`

### int put (Buffer & b, void \* o)` [related]`

## AUTHOR

*Generated automatically by Doxygen for CommonC++ from the source code.*
