Reputation: 11
Im suppose to use threads to populate an array with values (value is the same for all elements of array for each thread, but different threads have different values). In this code I come across a problem where when i set the start and no of elements that the thread has to populate in the array, it doesnt get set properly for the first thread, the rest seem to work fine.. could someone help me figure out where im going wrong, im suppose to do this without locks, help would be much appreciated!! Example no of threads = 2 Size of array = 3 the first thread will populate the first 2 elements and the second thread will populate the last 3 elements..
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int* array;
struct variables
{
int start;
int elements;
int value;
};
void* runner(void *param)
{
//int e=atoi((char*)param);
variables* a=(struct variables*)param;
cout<<"start="<<a->start<<endl;
cout<<"elements"<<a->elements<<endl;
cout<<"value"<<a->value<<endl;
for(int i=a->start, j=0; j<a->elements; i++, j++ )
{
array[i]=a->value;
}
}
int main()
{
pthread_t tid[10];
int noOfThreads, sizeOfArray;
cout<<"No of threads (between 1-20): ";
cin>>noOfThreads;
cout<<"size of array: ";
cin>>sizeOfArray;
array = new int[sizeOfArray];
int e=sizeOfArray/noOfThreads;
//int start=0;
for(int i=0,j=0; i<noOfThreads; i++)
{
variables v;
cout<<"i="<<i<<endl;
v.start=(e*j);
cout<<"v.start="<<v.start<<endl;
cout<<"j="<<j<<endl;
if(i==(noOfThreads-1))
{
e=sizeOfArray-(e*(noOfThreads-1));
cout<<"e="<<e<<endl;
}
cout<<"Enter the value for Thread "<<i+1<<":";
cin>>v.value;
v.elements=e;
pthread_create(&tid[i], NULL, runner,&v);
j++;
}
for(int i=0; i<noOfThreads; i++)
{
pthread_join(tid[i], NULL);
}
for(int i=0; i<noOfThreads; i++)
{
cout<<"tid="<<tid[i]<<endl;
}
cout<<"printing array"<<endl;
for(int i=0; i<sizeOfArray; i++)
{
cout<<array[i]<<" ";
}
}
Upvotes: 0
Views: 561
Reputation: 4761
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int* array;
struct variables
{
int start;
int elements;
int value;
};
void* runner(void *param)
{
//int e=atoi((char*)param);
variables* a=(struct variables*)param;
cout<<"start="<<a->start<<endl;
cout<<"elements="<<a->elements<<endl;
cout<<"value="<<a->value<<endl;
for(int i=a->start, j=0; j<a->elements; i++, j++ )
{
array[i]=a->value;
}
}
int main()
{
pthread_t tid[10];
int noOfThreads, sizeOfArray;
cout<<"No of threads (between 1-20): ";
cin>>noOfThreads;
cout<<"size of array: ";
cin>>sizeOfArray;
array = new int[sizeOfArray];
variables* v = new variables[noOfThreads];
int e=sizeOfArray/noOfThreads;
for (int i=0;i<noOfThreads;i++) {
v[i].start = e*i;
if(i==(noOfThreads-1)) {
v[i].elements=sizeOfArray-(e*(noOfThreads-1));
} else {
v[i].elements=e;
}
cout<<"Enter the value for Thread "<<i+1<<":";
cin>>v[i].value;
}
for(int i=0; i<noOfThreads; i++) {
pthread_create(&tid[i], NULL, runner,&v[i]);
}
for(int i=0; i<noOfThreads; i++)
{
pthread_join(tid[i], NULL);
}
for(int i=0; i<noOfThreads; i++)
{
cout<<"tid="<<tid[i]<<endl;
}
cout<<"printing array"<<endl;
for(int i=0; i<sizeOfArray; i++)
{
cout<<array[i]<<" ";
}
}
Upvotes: 1
Reputation: 4761
You create variables v
on stack, pass pointer to thread, v goes out of scope on loop iteration, you reallocate [same] memory and rewrite start elements and value, all threads will have same pointer (same structure), i believe, unless they exit faster than v expired. Put this in runner: cout << params
to check if i'm right.
Upvotes: 1