I have a function that uses four threads to work on different parts of an array, the problem that, I don't know shall I pass the array to the threads routine, here a code example:
#define RANGE_STEP1 1024 / 4
#define QUARTER RANGE_STEP1 / 4
struct thread_data
{
unsigned start;
unsigned stop;
};
#define NUM_THREADS 4
struct thread_data thread_data_array[NUM_THREADS];
pthread_t threads[NUM_THREADS];
void routine(void *thread_info)
{
int n;
unsigned t_start,t_stop;
struct thread_data *mydata;
mydata = (struct thread_data*) thread_info;
t_start = mydata->start;
t_stop = mydata->stop;
for (n = 0; n < RANGE_STEP1; n++)
{
result1[n] = (data1[n] + data2[n])/2; // Error : error: ‘data1’ undeclared (first use in this function) ...
result1[n] = (data1[n] - data2[n])/2; // Error . error: ‘data2’ undeclared (first use in this function) ...
}
pthread_exit(NULL);
}
void foo(float* data1, float* data2,float* result1,float* result2)
{
unsigned t,i=0;
for(t=0;t<RANGE_STEP1;t+=QUARTER)
{
thread_data_array[i].start = t;
thread_data_array[i].stop = t+QUARTER-1;
pthread_create(&threads[i],NULL,routine,(void *)&thread_data_array[i]);
i++;
}
pthread_exit(NULL);
}
So, as you can see in the example data1, data2, result1, result2 are the arrays that i want to the thread routine and I think it is not logical to pass it through the struct because it be re-written 4 times in the for() loop.