CMS 3D CMS Logo

WaitingTaskList Class Reference

#include <FWCore/Concurrency/interface/WaitingTaskList.h>


Detailed Description

Description: Handles starting tasks once some resource becomes available.

Usage: This class can be used to have tasks wait to be spawned until a resource is available. Tasks that want to use the resource are added to the list by calling add(tbb::task*). When the resource becomes available one calls doneWaiting() and then any waiting tasks will be spawned. If a call to add() is made after doneWaiting() the newly added task will immediately be spawned. The class can be reused by calling reset(). However, reset() is not thread-safe so one must be certain neither add(...) nor doneWaiting() is called while reset() is running.

An example usage would be if you had a task doing a long calculation (the resource) and then several other tasks have been created in a different thread and before running those new tasks you need the result of the long calculation.

 class CalcTask : public tbb::task {
    public:
    CalcTask(edm::WaitingTaskList* iWL, Value* v):
    m_waitList(iWL), m_output(v) {}
 
    tbb::task* execute() {
     *m_output = doCalculation();
     m_waitList.doneWaiting();
     return nullptr;
    }
    private:
     edm::WaitingTaskList* m_waitList;
     Value* m_output;
 };

In one part of the code we can setup the shared resource

 WaitingTaskList waitList;
 Value v;

In another part we can start the calculation

 tbb::task* calc = new(tbb::task::allocate_root()) CalcTask(&waitList,&v);
 tbb::task::spawn(calc);

Finally in some unrelated part of the code we can create tasks needed the calculation

 tbb::task* t1 = makeTask1(v);
 waitList.add(t1);
 tbb::task* t2 = makeTask2(v);
 waitList.add(t2);