cwidget  0.5.17
Public Member Functions | List of all members
cwidget::threads::box< T > Class Template Reference

A higher-level abstraction borrowed from Concurrent Haskell, which borrowed it from another language I forget. More...

#include <threads.h>

Public Member Functions

 box ()
 Create an empty box. More...
 
 box (const T &_val)
 Create a box containing the given value. More...
 
take ()
 Retrieve the current value of this box. More...
 
void put (const T &t)
 Fill this box with a value. More...
 
bool try_take (T &out)
 If there is a value in the box, retrieve it immediately; otherwise do nothing. More...
 
bool try_put (const T &t)
 If the box is empty, place a value in it; otherwise, do nothing. More...
 
bool timed_take (T &out, const timespec &until)
 As try_take(), but wait for the given amount of time before giving up.
 
bool timed_put (const T &t, const timespec &until)
 As try_put(), but wait for the given amount of time before giving up.
 
template<typename Mutator >
void update (const Mutator &m)
 Atomically modify the contents of the box; if an exception is thrown by the given function object, no action will be performed.
 

Detailed Description

template<typename T>
class cwidget::threads::box< T >

A higher-level abstraction borrowed from Concurrent Haskell, which borrowed it from another language I forget.

This represents a "box" that can either hold a value or be empty. Any thread can take the current value of the box or place a new value inside it; the attempt will block until a value is available or the box is empty, respectively. It's sort of a single-element bounded communications channel.

The value in the box is stored with copying semantics. Like the other threading primitives, boxes are not copyable.

Constructor & Destructor Documentation

◆ box() [1/2]

template<typename T>
cwidget::threads::box< T >::box ( )
inline

Create an empty box.

◆ box() [2/2]

template<typename T>
cwidget::threads::box< T >::box ( const T &  _val)
inline

Create a box containing the given value.

Member Function Documentation

◆ put()

template<typename T>
void cwidget::threads::box< T >::put ( const T &  t)
inline

Fill this box with a value.

If the box is full, block until it is empty.

Referenced by cwidget::threads::box< T *>::put().

◆ take()

template<typename T >
T cwidget::threads::box< T >::take ( )
inline

Retrieve the current value of this box.

If the box is empty, block until it is full.

Referenced by cwidget::threads::box< T *>::take().

◆ try_put()

template<typename T>
bool cwidget::threads::box< T >::try_put ( const T &  t)
inline

If the box is empty, place a value in it; otherwise, do nothing.

Parameters
tthe value to place in the box
Returns
true iff the box was empty (and hence was filled with t)

Referenced by cwidget::threads::box< T *>::try_put().

◆ try_take()

template<typename T>
bool cwidget::threads::box< T >::try_take ( T &  out)
inline

If there is a value in the box, retrieve it immediately; otherwise do nothing.

Parameters
outthe location in which the value should be stored
Returns
true iff a value was found in the box

Referenced by cwidget::threads::box< T *>::try_take().


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