Hi!
A task was to solve a problem using threads (sum up two arrays), but so that you have a global const which will represent number of threads you want to use. Somehow, this doesn't work, and Im not sure if this is correct way to do it.
Thanks for your help.

#include<iostream>
#include<pthread.h>

using namespace std;

int *a; 
int *b; 
int *c;

const int N = 10;
const int n=2;

void* add(void* start) 
{
       int tid = *(int*)start;
       while (tid < N) 
       {
              c[tid] = a[tid] + b[tid];
              tid += n+1; 
        }
}


int main()
{
 a = new int[10];
 b = new int[10];
 c = new int[10];

 for (int i=0;i<10;i++)
 {
    a[i] = i;
    b[i] = 2*i;
 }

  pthread_t tID[n];
  int t[n];

  for(int i=0;i<n;i++)
    t[i]=pthread_create(&tID[i],NULL,add,(void*)i);

  for(int i=0;i<n;i++)
    pthread_join(tID[i], NULL);

 cout << "a = ";
 for (int i=0;i<N;i++)
   cout << a[i] << "  " ;
 cout << endl;
 cout << "b = " ;
 for (int i=0;i<N;i++)
   cout << b[i] << "  ";
 cout << endl;

 cout << "a + b = " << endl;
 for (int i=0;i<N;i++)
   cout <<"c["<<i << "] = " << c[i] <<endl;


 return 1;
}

Recommended Answers

All 6 Replies

Are you sure "sum up two arays" means a[tid] + b[tid] instead of one thread sums the values of array a and the other array sums the values in array b?

Yes, maybe I didn't explain well (add up 2 vectors is perhaps better explanation)

It seems the two threads are overlapping the arrays. The first thread should add elements 0-4 and the second thread should add elements 5-9. And array C should only have 2 elements, not 10.

The loop on line 10 should run only 5 times, not 10 times because each thread is summing only 1/2 the number of elements in each array.

The last parameter on line 40 would also be incorrect. When i == 2 the last parameter should be 5, not 2 so that sum() starts summing the two arrays at element #5, not element #2.

Finally, why are the three arrays pointers and memory allocated at runtime? That is ok for huge arrays, but quite a waste of time/memory for small arrays. Lines 6-8 could simply be this:

const int N = 10;
const int n=2;
int a[N];
int b[N];
int c[n];

No, this is not problem. Matematically, if "a" and "b" are vectors with "n" components, then c=a+b is a vector with same number of components, and c[i]=a[i]+b[i].
Threads are used in such way so that they add only some of those numbers.
(eg, in that example with 2 threads, first thread should do c[tid]=a[tid]+b[tid] for even numbers (because "start" variable is 0 and tid would increase by 2 every time), and second thread should do it for odd numbers (so, first thread calculates c[0], c[2], c[4],...,c[8], and second thread calculates c[1], c[3],c[5],...,c[9]).
I think there is some problem with array of pthreads in main function, cause nothing is printed on screen after running the program.

I guess I misunderstood the assignment. To me, "sum up the vectors" means c[0] = sum(a) + sum(b)

Sorry, I can't test your program because I'm using MS-Windows and pthread.h is not supported there. c++11 standards supports threads directly now so you don't need pthreads.h or associated library. If you are using gcc compiler then the latest version contains support for c++11 standards.

This was the problem

for(int i=0;i<n;i++)
    t[i]=pthread_create(&tID[i],NULL,add,(void*)i);

Because "i" is int, not pointer to int, so i changed it to

  int* start=new int(0);

  for(int i=0;i<n;i++)
  {
    t[i]=pthread_create(&tID[i],NULL,add,(void*)start);
    (*start)++;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.