Pass data efficiently between C ++ classes

Help is needed...

I have 3 classes, a manager that contains 2 pointers. One for class A the other for class B. A does not know about B and vice versa.

A does some calculations, and at the end it puts 3 floats on the clipboard. Then B pulls 3 floats from the clipboard and performs its own calculations. This cycle is managed by the manager and is repeated many times (iteration after iteration).

My problem: Now class A creates a floating-point vector that needs class B. This vector can have more than 1000 values, and I don’t want to use the clipboard to transfer it to B, because it will become a consumer of time, even a bottleneck, because it behavior is repeated step by step.

A simple solution is that B will know A (set the pointer to A). Another is the transfer of the vector pointer through the Manager. But I'm looking for something else, more oriented to the object, which will not break the existing separation between A and B

Any ideas?

Many thanks

David

+4
source share
6 answers

It looks like you are writing a pair of producers / consumers that can more easily communicate in turns (possibly thread safe) of floats.

In other words: "queue" is similar to the vector you are using. Both A and B will refer to this queue. Performs calculations and writes floats to the queue (maybe three at a time, if necessary). B checks the queue or, possibly, “signals” A that the queue is ready, and grabs the floats from the queue to process them.

For more google information (or search) for “consumer-producer” and / or “lineup”, you will probably find a lot of useful information.

(e.g. C ++ multithreaded work queue )

+9
source

clipboard is a crazy way to transfer data between two objects.

If B implements a common interface such as IConsumer, the manager can pass this interface to A, and A can directly call method B with the payload:

class IConsumer { public: virtual void consume(const vector<float>& data) = 0; }; class B: public IConsumer { public: virtual void consume(const vector<float>& data) { ... } ... }; class A { public: virtual void produce(IConsumer& consumer) { vector<float> data; ... consumer.consume(data); } ... }; void Manager::tick() { a->produce(*b); } 
+3
source

Create a vector and pass it using the "pass by reference" for A and B in your iterations.

+1
source

B.method (A.method ());

+1
source

The problem is resolved after you realize that there should not be class A and class B to manage the shared data store, as it is no exception. Therefore, you are right in thinking that Manager should do this.

For maximum performance, do not pass a pointer to a vector in A and B. Instead, pass a raw float* , as this preserves the level of indirection. The compromise should be to pass boost::array &

0
source

As already mentioned, you can simply use std::vector<float> as a common repository if A and B do not start on different threads. Manager can manage this repository. When A begins its work, it can request a range from this vector to save the results. When this is done, Manager will call B and give it the range that has just been filled with the values ​​of A This avoids copying.

0
source

Source: https://habr.com/ru/post/1308643/


All Articles