MPD: WorkQueue< T > Class Template Reference

MPD 0.20.15

Public Member Functions
WorkQueue< T > Class Template Reference

A WorkQueue manages the synchronisation around a queue of work items, where a number of client threads queue tasks and a number of worker threads take and execute them. More...

`#include <WorkQueue.hxx>`

## Public Member Functions
| WorkQueue (const char *_name)
| Create a WorkQueue. More...
| ~WorkQueue ()
| WorkQueue (const WorkQueue &)=delete
WorkQueue & | operator= (const WorkQueue &)=delete
bool | start (unsigned nworkers, void *(*workproc)(void *), void *arg)
| Start the worker threads. More...
template<typename U >
bool | put (U &&u)
| Add item to work queue, called from client. More...
void | setTerminateAndWait ()
| Tell the workers to exit, and wait for them. More...
bool | take (T &tp)
| Take task from queue. More...
void | workerExit ()
| Advertise exit and abort queue. More...

## Detailed Description

### template<class T>
class WorkQueue< T >

A WorkQueue manages the synchronisation around a queue of work items, where a number of client threads queue tasks and a number of worker threads take and execute them.

The goal is to introduce some level of parallelism between the successive steps of a previously single threaded pipeline. For example data extraction / data preparation / index update, but this could have other uses.

There is no individual task status return. In case of fatal error, the client or worker sets an end condition on the queue. A second queue could conceivably be used for returning individual task status.

Definition at line 49 of file WorkQueue.hxx.

## Constructor & Destructor Documentation

## ◆ WorkQueue() [1/2]

template<class T>

WorkQueue< T >::WorkQueue | ( | const char * | _name | ) |
| inlineexplicit

Create a WorkQueue.

Parameters

  _name | for message printing

Definition at line 71 of file WorkQueue.hxx.

## ◆ ~WorkQueue()

template<class T>

WorkQueue< T >::~WorkQueue | ( |  | ) |
| inline

Definition at line 76 of file WorkQueue.hxx.

## ◆ WorkQueue() [2/2]

template<class T>

WorkQueue< T >::WorkQueue | ( | const WorkQueue< T > & |  | ) |
| delete

## Member Function Documentation

## ◆ operator=()

template<class T>

WorkQueue& WorkQueue< T >::operator= | ( | const WorkQueue< T > & |  | ) |
| delete

## ◆ put()

template<class T>
template<typename U >

bool WorkQueue< T >::put | ( | U && | u | ) |
| inline

Add item to work queue, called from client.

Sleeps if there are already too many.

Definition at line 121 of file WorkQueue.hxx.

## ◆ setTerminateAndWait()

template<class T>

void WorkQueue< T >::setTerminateAndWait | ( |  | ) |
| inline

Tell the workers to exit, and wait for them.

Definition at line 136 of file WorkQueue.hxx.

## ◆ start()

template<class T>

bool WorkQueue< T >::start | ( | unsigned | nworkers,
|  | void *(*)(void *) | workproc,
|  | void * | arg
| ) |  |
| inline

Start the worker threads.

Parameters

  nworkers | number of threads copies to start.
  workproc | thread function. It should loop taking (QueueWorker::take()) and executing tasks.
  arg | initial parameter to thread function.

Returns
  true if ok.

Definition at line 91 of file WorkQueue.hxx.

## ◆ take()

template<class T>

bool WorkQueue< T >::take | ( | T & | tp | ) |
| inline

Take task from queue.

Called from worker.

Sleeps if there are not enough. Signal if we go to sleep on empty queue: client may be waiting for our going idle.

Definition at line 167 of file WorkQueue.hxx.

## ◆ workerExit()

template<class T>

void WorkQueue< T >::workerExit | ( |  | ) |
| inline

Advertise exit and abort queue.

Called from worker

This would happen after an unrecoverable error, or when the queue is terminated by the client. Workers never exit normally, except when the queue is shut down (at which point ok is set to false by the shutdown code anyway). The thread must return/exit immediately after calling this.

Definition at line 193 of file WorkQueue.hxx.

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

- lib/upnp/WorkQueue.hxx