3

I need to send some "heavy" types between nodes in tbb::flow::graph (Intel TBB library), ie structure with dynamic arrays within. If I try to create an instance of such structure in one node and send a pointer on it to the other node, I get Access Violation (and this is expected, cause I try to use the data from another thread).

So only way to pass such arguments is to pass them by value using appropriate copy-constructor, isn't it? But then we will waste a lot of time for copying...

UPDATE: πάντα ῥεῖ suggests to use std::unique_ptr to pass such types through nodes. But I'm afraid I don't understand how to implement that. For example, how to use std::unique_ptr with source_node?

UPDATE 2: using std::unique_ptr<> gives C2280 'attempting to reference a deleted function' within flow_graph.h line 287.

So question is still opened.

Max Pashkov
  • 404
  • 1
  • 3
  • 12

1 Answers1

5

Max,

Unfortunately the std::unique_ptr does not have the property CopyConstructible, which is a requirement for object messages being passed by flow::graph.

You might try std::shared_ptr, which does have the property. We have fixed problems with retention of objects in the buffers (which caused large objects to be retained until the graph was reset() ). If you find any problems with it, please let us know.

Regards, Chris

cahuson
  • 826
  • 4
  • 10
  • Thanks for your attention, I've already rewritten my code using std::share_ptr and it works. – Max Pashkov Oct 30 '15 at 15:34
  • Good solution. However one case in which using a shared_ptr for messages between graph nodes causes problems is when using an async_node where the gateway.try_put(message) is called from a different background thread. If message is a shared_ptr then there is a potential race condition on its reference count between the background thread and the rest of the flow graph. – persiflage Apr 01 '21 at 10:02
  • @persiflage I believe it should not be a problem as the control block of std::shared_ptr is supposed to be thread-safe. – Floop Mar 17 '22 at 21:10