I am sort of new to forums, therefore forgive me for mistakes. I am trying to eliminate a for loop within a function and run the function in multiple threads. Is it possible?. The comment inside the code, explains it. Any reasonsble change that results in a good
multi-threading.
#include <iostream>
#include <windows.h>
class A
{
public:
A()
{};
~A()
{};
void set_array()
{
for (int i = 0; i < 10; i++)
{
m_arr[i] = i;
}
}
void get_array(int arr[])
{
for (int i = 0; i < 10; i++)
{
arr[i] = m_arr[i];
}
}
private:
int m_arr[10];
};
class B
{
public:
B()
{};
~B()
{};
void copy_array(int arr[])
{
for (int i = 0; i < 10; i++)
{
m_arr[i] = arr[i];
}
}
int get_array(int arr[])
{
for (int i = 0; i < 10; i++)
{
arr[i] = m_arr[i];
}
}
int do_something_with_array(void);
private:
int m_arr[10];
};
//Comment: I like to take the first for loop out and
//split this function into 6 different threads.
//c loops and gets passed to each thread.
int B::do_something_with_array(void)
{
int OutputArray[10] = {0};
for (size_t c = 0; c < 6; c++)
{
for (size_t d = 0; d < 10; d++)
{
OutputArray[d] = m_arr[d] + (int)c;
}
}
for (size_t i = 0; i < 10; i++)
{
std::cout << OutputArray[i] << std::endl;
}
return 0;
}
int main(int argc, char* argv[])
{
int aArr[10] = {0};
A a;
a.set_array();
a.get_array(aArr);
B b;
b.copy_array(aArr);
b.do_something_with_array();
return 0;
}