I have a thread that calculates data in an endless loop. it produces three results
- a std::vector (behind each vector element there is a float array of 3 elements on the heap)
- an int array*
- an int that indicates the size of the int[].
two other threads should now process this data (also endless). Since all three threads do not take the same processing time, data is sometimes "skipped".
std::vector<float*> a_vecor;
int* a_Array;
int a_Array_size;
std::mutex a_mutex;
void thread_A()
{
while (true)
{
calculate();
a_mutex.lock();
a_vector = getvector();
a_Array = getArray();
a_Array_size = getArraysize();
a_mutex.unlock();
}
}
void thread_B()
{
while (true)
{
a_mutex.lock();
std::vector<float*> b_vector = a_vector;
int* b_Array = a_Array;
int b_array_Size = a_array_Size;
a_mutex.unlock();
calculate_b();
}
}
void thread_C()
{
while (true)
{
a_mutex.lock();
std::vector<float*> c_vector = a_vector;
int* c_Array = a_Array;
int c_array_Size = a_array_Size;
a_mutex.unlock();
calculate_c();
}
}
My problem is how can I pass this data from one thread to another? I would actually make a copy of the three parameters in the following thread, but I can just:
std::vector <float*> b = calculate();
std::vector <float*> a = b;
is this a copy or a reference? how about the vector elements which are only references? what is the fastest way to copy a vector and array? Doesn't a loop run internally here too? the flow of information is only in one direction, one process generates the data, the next only reads it. can i take advantage of this? is there an alternative to the mutex?