Taskflow  3.2.0-Master-Branch
Loading...
Searching...
No Matches
tf::ObserverInterface Class Referenceabstract

class to derive an executor observer More...

#include <observer.hpp>

Inheritance diagram for tf::ObserverInterface:

Public Member Functions

virtual ~ObserverInterface ()=default
 virtual destructor
 
virtual void set_up (size_t num_workers)=0
 constructor-like method to call when the executor observer is fully created
 
virtual void on_entry (WorkerView w, TaskView task_view)=0
 method to call before a worker thread executes a closure
 
virtual void on_exit (WorkerView w, TaskView task_view)=0
 method to call after a worker thread executed a closure
 

Friends

class Executor
 

Detailed Description

class to derive an executor observer

The tf::ObserverInterface class let users define custom methods to monitor the behaviors of an executor. This is particularly useful when you want to inspect the performance of an executor and visualize when each thread participates in the execution of a task. To prevent users from direct access to the internal threads and tasks, tf::ObserverInterface provides immutable wrappers, tf::WorkerView and tf::TaskView, over workers and tasks.

Please refer to tf::WorkerView and tf::TaskView for details.

Example usage:

struct MyObserver : public tf::ObserverInterface {
MyObserver(const std::string& name) {
std::cout << "constructing observer " << name << '\n';
}
void set_up(size_t num_workers) override final {
std::cout << "setting up observer with " << num_workers << " workers\n";
}
void on_entry(WorkerView w, tf::TaskView tv) override final {
oss << "worker " << w.id() << " ready to run " << tv.name() << '\n';
std::cout << oss.str();
}
void on_exit(WorkerView w, tf::TaskView tv) override final {
oss << "worker " << w.id() << " finished running " << tv.name() << '\n';
std::cout << oss.str();
}
};
tf::Taskflow taskflow;
tf::Executor executor;
// insert tasks into taskflow
// ...
// create a custom observer
std::shared_ptr<MyObserver> observer = executor.make_observer<MyObserver>("MyObserver");
// run the taskflow
executor.run(taskflow).wait();
class to create an executor for running a taskflow graph
Definition executor.hpp:50
tf::Future< void > run(Taskflow &taskflow)
runs a taskflow once
Definition executor.hpp:1573
std::shared_ptr< Observer > make_observer(ArgsT &&... args)
constructs an observer to inspect the activities of worker threads
Definition executor.hpp:1038
class to derive an executor observer
Definition observer.hpp:169
class to access task information from the observer interface
Definition task.hpp:638
class to create a taskflow object
Definition core/taskflow.hpp:73

Member Function Documentation

◆ on_entry()

virtual void tf::ObserverInterface::on_entry ( WorkerView  w,
TaskView  task_view 
)
pure virtual

method to call before a worker thread executes a closure

Parameters
wan immutable view of this worker thread
task_viewa constant wrapper object to the task

◆ on_exit()

virtual void tf::ObserverInterface::on_exit ( WorkerView  w,
TaskView  task_view 
)
pure virtual

method to call after a worker thread executed a closure

Parameters
wan immutable view of this worker thread
task_viewa constant wrapper object to the task

◆ set_up()

virtual void tf::ObserverInterface::set_up ( size_t  num_workers)
pure virtual

constructor-like method to call when the executor observer is fully created

Parameters
num_workersthe number of the worker threads in the executor

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