Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer data between C++ classes efficiently

Tags:

c++

object

oop

Need help...

I have 3 classes, Manager which holds 2 pointers. One to class A another to class B . A does not know about B and vise versa.

A does some calculations and at the end it puts 3 floats into the clipboard. Next, B pulls from clipboard the 3 floats, and does it's own calculations. This loop is managed by the Manager and repeats many times (iteration after iteration).

My problem: Now class A produces a vector of floats which class B needs. This vector can have more than 1000 values and I don't want to use the clipboard to transfer it to B as it will become time consumer, even bottleneck, since this behavior repeats step by step.

Simple solution is that B will know A (set a pointer to A). Other one is to transfer a pointer to the vector via Manager But I'm looking for something different, more object oriented that won't break the existent separation between A and B

Any ideas ?

Many thanks

David

like image 682
David Avatar asked Dec 09 '22 16:12

David


1 Answers

It kind of sounds like you are writing a producer / consumer pair, who may communicate more easily over a (probably thread-safe) queue of floats.

In other words: the "queue" is like the vector you are currently using. Both A and B will have a reference to this queue. A runs calculations, and writes floats to the queue (possibly three at a time, if that's what you need). B checks the queue, or possibly is "signaled" by A that the queue is ready, and grabs the floats from the queue to process them.

For more info, google (or search Stack Overflow) for "producer consumer" and/or "queue", you'll probably find a lot of useful info.

(e.g. Multithreaded Work Queue in C++)

like image 183
Joris Timmermans Avatar answered Dec 30 '22 04:12

Joris Timmermans