I have data, confined in a struct
called transfer_data
:
struct transfer_data{
std::vector<int> data;
}
and I have vectors pointing on that data:
transfer_data dat1, dat2;
transfer_data *ptr_dat1 = &dat1, *ptr_dat2 = &dat2;
Now my function is fixed onto always reading data from a copy of one pointer, while the data belonging to the other one is processed in a second thread. When the data processing is finished, the first pointer is exchanged with the second one by the second thread.
I wanted to use that approach in order not to copy too much data (>1 Mbyte) around when the second thread is finished processing, and to avoid race conditions (first thread is reading, second thread is writing on the same place etc.). Thus by copying only vectors it should be way faster.
My problem is: How can I exchange non-global pointers in a function? If I want to do that for variables, I write
void switch(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
When writing a similar thing for pointer:
void switch(int *a, int *b)
{
int *tmp = a;
a = b;
b = tmp;
}
But if I am doing that, then I only switch the pointer values locally, and not globally, i.e. returning from the function means returning to status quo.
How can I fix my function? Or is there a more fundamental flaw in my approach?