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 :)
creator
... You know show the code for its parent class.true (72)
? Does that mean the object-representation was72
, not1
? If so, that's further evidence (beyond a value changing when you don't expect it to) that you have a memory over-write bug somewhere. An actualbool
is required by both mainstream x86-64 ABIs (Windows and SysV) to be0
or1
. (Does the C++ standard allow for an uninitialized bool to crash a program?). Set a watchpoint on thatatomic<bool>
and see what changes it.atomic_bool
, though, you're capturing astd::shared_ptr<atomic_bool>
local variable. It was constructed from the member, but itself is a local variable. (I don't see why you need all this complexity, or ashared_ptr<atomic_bool>
member variable instead of just anatomic_bool
member variable right in the class object, but maybe you have your reasons for wanting an extra level of indirection and the smart-pointer with control-block to manage it. Concurrent access to the same shared_ptr object is not safe, though, only access to the same pointed-to atomic_bool)std::shared_ptr
itself isn't safe to access from multiple threads, except its const members. (Not sure which those include, hopefully deref for read-write access to the underlying object is ok). But the underlyingatomic_bool
itself can be concurrently accessed in any way by multiple threads at once. It's not clear to me whether your usage pattern might have any data-race bugs on the shared_ptr object.