1

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.

2
  • You actual question is a bit unclear, perhaps you can work on it a bit. The use of "not logical to ... otherwise ..." makes little sense to be. I'm not a native speaker, though. Commented Nov 22, 2017 at 11:16
  • 2
    Put them in the struct. They're only pointers and the effort required to write them is swamped by that required to actually create the threads and, hopefully, by the cycles used by the threads when working on their bit of the array. If it was me, I would malloc each struct each time round the loop anyway, dumping the array of structs and completely encapsulating the data for each thread. This 'declare a fixed array for thread struct data and pthread_t's, call join() after' design is now deeply ingrained on intro sites. I wish they would all die a horrible death:) Commented Nov 22, 2017 at 13:52

1 Answer 1

1

If you don't want to pass it through the struct (by adding and setting elements for data and results), you can pass them via global variables, e. g.

static float *data1, *data2, *result1, *result2;

void *routine(void *thread_info)
{
   …
}

void foo(float *Data1, float *Data2, float *Result1, float *Result2)
{
    data1 = Data1, data2 = Data2, result1 = Result1, result2 = Result2;
   …
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.