TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
|
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_ |
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.
using common::BufferedConsumer< T >::type = T |
common::BufferedConsumer< T >::BufferedConsumer | ( | uint8_t | nthreads, |
size_t | maxQueueSize, | ||
Function && | fn | ||
) |
common::BufferedConsumer< T >::~BufferedConsumer | ( | ) |
Stops the consumers, discarding any items in the queue.
void common::BufferedConsumer< T >::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).
void common::BufferedConsumer< T >::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.
void common::BufferedConsumer< T >::run | ( | ) |
void common::BufferedConsumer< T >::wait | ( | ) |
Blocks until the queue is empty or the consumers are stopped.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |