I am doing something like this with TBB:
#include <tbb/tbb.h>
#include <memory>
#include <atomic>
class base_creator
{
public:
using node = tbb::flow::input_node<bool>;
virtual ~base_creator() = default;
base_creator()
{
m_kill = std::make_shared<std::atomic_bool>(false);
};
static tbb::flow::graph& g()
{
static tbb::flow::graph me_g;
return me_g;
};
virtual std::shared_ptr<node> get_node() const = 0;
template<typename Op>
static tbb::flow::input_node<bool> build_node(const Op& op)
{
return tbb::flow::input_node<bool>(base_creator::g(), op);
};
protected:
mutable std::shared_ptr<std::atomic_bool> m_kill;
};
class creater : public base_creator
{
public:
creater() = default;
public:
virtual std::shared_ptr<node> get_node() const override
{
const std::shared_ptr<std::atomic_bool> flag = this->m_kill;
auto op = [flag](flow_control& control) -> bool
{
if (flag->load(std::memory_order_relaxed))
control.stop();
return true;
};
node nd = base_creator::build_node(std::cref(op));
return std::make_shared<node>(nd);
};
};
int main(int argc, char* argv[])
{
creater c;
std::shared_ptr<base_creator::node> s = c.get_node();
using my_func_node = std::shared_ptr<tbb::flow::function_node<bool, bool>>;
my_func_node f = std::make_shared<tbb::flow::function_node<bool, bool>>(base_creator::g(), 1,[](const bool b) { std::cout << b; return b; });
tbb::flow::make_edge(*s, *f);
};
The flag should be always false
in this code. Once I call tbb::flow::make_edge
it becomes true
when debugging the body of the node s in THAT IS SO WEIRD? I have no clue, could you please help, I am starting to hit in the TBB code now and it's too complex :)