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;
}