KathyLee
KathyLee

Reputation: 61

Synchronize functions using pthread to do some simple operations on an array

I am studying pthread but confused about how to use pthread to synchronize the functions.

For example, I have a simple code to do some operations on an array like following:

float add(int numbers[5]){
  float sum;
  for(int i = 0; i < 5; i++){
  sum = sum + numbers[i] +5;
  }
  return sum/5;
}
float subtract(int numbers[5]){
  float sum;
  for(int i = 0; i < 5; i++){
    sum = sum + numbers[i] -10;
  }
  return sum/5;
}
float mul(int numbers[5]){
  float sum;
  for(int i = 0; i < 5; i++){
    sum = sum + (float)numbers[i] * 1.5 ;
  }
  return sum/5;
}
float div(int numbers[5]){
  float sum;
  for(int i = 0; i < 5; i++){
    sum = sum + (float)numbers[i]/ 2;
  }
  return sum/5;
}

int main(){
  int numbers [5] = { 34, 2, 77, 40, 12 }; 
  float addition = add(numbers);

  float subtraction = subtract(numbers);

  float multiplication = mul(numbers);

  float division = div(numbers);

  cout << addition + subtraction + multiplication + division << endl;
    return -1;
}

Since all the four functions are independent from each other and using the same input, how can I put each operation into one thread and let the functions(or threads) run at the same time? I think if one day I have a very large array and run the program like above, it will spend a lot time but if I can make the functions run simultaneously, it will save a lot time.

Upvotes: 0

Views: 146

Answers (1)

SergeyA
SergeyA

Reputation: 62583

First of all, I suspect, you are not clear how arrays are passed into functions. float subtract(int numbers[5]) does not tell anything about size of the passed array. It is equivalent of float subtract(int numbers[]) (no size), which, in turn, is equivalent to ``float subtract(int* numbers)` (pointer to int). You also have a bug in your function, since you do not initialize float before first use (as well as other functions).

Having this in mind, the whole substract function is better to be written like this:

float subtract(int* numbers, const size_t size) {
  float sum = 0;
  for(int i = 0; i < size; i++) {
    sum = sum + numbers[i] -10;
  }
  return sum/5;
}

Now, once we cleared the function itself, we can tackle multithreading. I really suggest to ditch pthreads and instead use C++11 thread capability. That is especially true when you need to get the result back as a return value of the function. Doing it with pthreads would require too much typing. The relevant code to do this in C++ would be looking similar to this:

int numbers[] = {34, 2, 77, 40, 12}; // No need to provide array size when inited
auto sub_result = std::async(std::launch::async, &subtract, numbers, sizeof(numbers) / sizeof(*numbers);
auto div_result = .... 
// rest of functions
std::cout << "Result of subtraction: " << div_result.get();

Now, this is a lot to grasp :) std::async is a way to run the function asynchronously without worring about multithreading at all. The task of threading is delegated to the compiler. It is a much cleaner way than using pthreads - see, invocation is not much different from normal function invocation! The only thing to keep in mind is that it returns so-called std::future object - an special object on which you can wait until the function which was run completes execution. It also has a get function, which waits until the function is completed and returns it's result. Nice, eh?

Upvotes: 2

Related Questions