1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file Copyright 2009 QMUL.
10 #ifndef _ASYNCHRONOUS_TASK_H_
11 #define _ASYNCHRONOUS_TASK_H_
18 * AsynchronousTask provides a thread pattern implementation for
19 * threads which are used to perform a series of similar operations in
20 * parallel with other threads of the same type.
22 * For example, a thread used to calculate FFTs of a particular block
23 * size in the context of a class that needs to calculate many block
24 * sizes of FFT at once may be a candidate for an AsynchronousTask.
26 * The general use pattern is:
28 * caller -> request thread A calculate something
29 * caller -> request thread B calculate something
30 * caller -> request thread C calculate something
31 * caller -> wait for threads A, B, and C
33 * Here threads A, B, and C may be AsynchronousTasks. An important
34 * point is that the caller must be prepared to block when waiting for
35 * these threads to complete (i.e. they are started asynchronously,
36 * but testing for completion is synchronous).
38 class AsynchronousTask : public Thread
42 m_todo("AsynchronousTask: task to perform"),
43 m_done("AsynchronousTask: task complete"),
49 virtual ~AsynchronousTask()
58 // Subclass must provide methods to request task and obtain
59 // results, which the caller calls. The method that requests a
60 // new task should set up any internal state and call startTask(),
61 // which then calls back on the subclass implementation of
62 // performTask from within its work thread. The method that
63 // obtains results should call awaitTask() and then return any
64 // results from internal state.
79 virtual void performTask() = 0;
85 while (!m_inTask && !m_finishing) {