TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
Public Types | Public Member Functions | Protected Attributes | List of all members
common::BufferedConsumer< T > Class Template Reference

A simple producer/consumer class. More...

#include <parallel.h>

Public Types

using type = T
 

Public Member Functions

 BufferedConsumer (uint8_t nthreads, size_t maxQueueSize, Function &&fn)
 
 ~BufferedConsumer ()
 Stops the consumers, discarding any items in the queue. More...
 
void wait ()
 Blocks until the queue is empty or the consumers are stopped. More...
 
void enqueue (T arg)
 Adds another item to the work queue, possibly blocking If the number of threads is zero, execute directly in the calling thread's context (and thus block). More...
 
void enqueueOrReplaceOldest (T arg)
 Same as 'enqueue', except that if the queue is full, the oldest element will be removed before inserting Only works for nthreads > 0. More...
 
void run ()
 

Protected Attributes

size_t const maxQueueSize_
 
bool stop_ = false
 
int64_t consuming_ = 0
 
Function fn_
 
std::vector< std::thread > threads_
 
std::queue< T > queue_
 
std::mutex mutex_
 
std::condition_variable itemReady_
 
std::condition_variable itemDone_
 

Detailed Description

template<typename T>
class common::BufferedConsumer< T >

A simple producer/consumer class.

This class is dead-simple, but sometimes useful. You specify the element type for the queue in the type, and then instantiate it with functor which will run in a separate thread. The main function of the class is enqueue(), which, well, adds stuff to the queue. You also specify a maximum queue size on construction; if that size is reached, enqueue() will block.

As a special case, you can use this class with 0 threads. This means that the supplied functor will be called directly in the thread calling enqueue(). Items will be buffered implicitly by enqueue() blocking until consumption.

If you want to wait for the consumers to finish, call wait(). If you want to stop the consumer threads, destruct the object.

The implementation assumes that objects of type T are in a valid state (i.e. can be destructed) after moving. If that's not the case for your type, go fix your type.

Member Typedef Documentation

template<typename T>
using common::BufferedConsumer< T >::type = T

Constructor & Destructor Documentation

template<typename T >
common::BufferedConsumer< T >::BufferedConsumer ( uint8_t  nthreads,
size_t  maxQueueSize,
Function &&  fn 
)
template<typename T >
common::BufferedConsumer< T >::~BufferedConsumer ( )

Stops the consumers, discarding any items in the queue.

Member Function Documentation

template<typename T>
void common::BufferedConsumer< T >::enqueue ( arg)

Adds another item to the work queue, possibly blocking If the number of threads is zero, execute directly in the calling thread's context (and thus block).

template<typename T>
void common::BufferedConsumer< T >::enqueueOrReplaceOldest ( arg)

Same as 'enqueue', except that if the queue is full, the oldest element will be removed before inserting Only works for nthreads > 0.

template<typename T >
void common::BufferedConsumer< T >::run ( )
template<typename T >
void common::BufferedConsumer< T >::wait ( )

Blocks until the queue is empty or the consumers are stopped.

Member Data Documentation

template<typename T>
int64_t common::BufferedConsumer< T >::consuming_ = 0
protected
template<typename T>
Function common::BufferedConsumer< T >::fn_
protected
template<typename T>
std::condition_variable common::BufferedConsumer< T >::itemDone_
protected
template<typename T>
std::condition_variable common::BufferedConsumer< T >::itemReady_
protected
template<typename T>
size_t const common::BufferedConsumer< T >::maxQueueSize_
protected
template<typename T>
std::mutex common::BufferedConsumer< T >::mutex_
protected
template<typename T>
std::queue<T> common::BufferedConsumer< T >::queue_
protected
template<typename T>
bool common::BufferedConsumer< T >::stop_ = false
protected
template<typename T>
std::vector<std::thread> common::BufferedConsumer< T >::threads_
protected

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