CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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(oneapi::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 edm::WaitingTask {
public:
CalcTask(edm::WaitingTaskList* iWL, Value* v):
m_waitList(iWL), m_output(v) {}
oneapi::tbb::task* execute() {
std::exception_ptr ptr;
try {
m_output = doCalculation();
} catch(...) {
ptr = std::current_exception();
}
m_waitList.doneWaiting(ptr);
return nullptr;
}
private:
edm::WaitingTaskList* m_waitList;
Value* m_output;
};

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

WaitingTaskList waitList;

In another part we can start the calculation

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

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

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