template<typename T, typename FMap>
class bm::QueueingLogic< T, FMap >
One of the most basic queueing block possible. Supports an arbitrary number of logical queues (identified by arbitrary integer ids). Lets you choose (at runtime) the number of worker threads that will be reading from these queues. I write "logical queues" because the implementation actually uses as many physical queues as there are worker threads. However, each logical queue still has its own maximum capacity. As of now, the behavior is blocking for both read (pop_back()) and write (push_front()), but we may offer additional options if there is interest expressed in the future.
Template parameter T
is the type (has to be movable) of the objects that will be stored in the queues. Template parameter FMap
is a callable object that has to be able to map every logical queue id to a worker id. The following is a good example of functor that meets the requirements:
struct WorkerMapper {
WorkerMapper(size_t nb_workers)
: nb_workers(nb_workers) { }
size_t operator()(size_t queue_id) const {
return queue_id % nb_workers;
}
size_t nb_workers;
};
template<typename T , typename FMap >
nb_workers
is the number of threads that will be consuming from the queues; they will be identified by an id in the range [0, nb_workers)
. capacity
is the number of objects that each logical queue can hold. Because we need to be able to map each queue id to a worker id, the user has to provide a callable object of type FMap
, map_to_worker
, that can do this mapping. See the QueueingLogic class description for more information about the FMap
template parameter.
template<typename T , typename FMap >
void bm::QueueingLogic< T, FMap >::pop_back |
( |
size_t |
worker_id, |
|
|
size_t * |
queue_id, |
|
|
T * |
pItem |
|
) |
| |
|
inline |
Retrieves the oldest element for the worker thread indentified by worker_id
and moves it to pItem
. The id of the logical queue which contained this element is copied to queue_id
. As a remainder, the map_to_worker
argument provided when constructing the class is used to map every queue id to the corresponding worker id. Therefore, if an element E
was pushed to queue queue_id
, you need to use the worker id map_to_worker(queue_id)
to retrieve it with this function.