Thread programming

Making a thread program which takes segments of an array then processes them with a computation in multiple threads. Will be testing between 1-8. The array is of a big size so that it takes some time to process so that I can see changes when I add threads.

I should be finished I believe but I have issues.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sys/time.h>
#include "hr_time.h"
#include <pthread.h>

#define ARRAYSIZE 1000000
#define THREADS 2

using namespace std;

struct arrayStruct {
int * dataArray;
int aStart;
int length;
};

//-----Thread Function-----
void* thread_function(void* psegment)
{
arrayStruct* segment = (arrayStruct*)psegment;

for (int i = segment->aStart; i < (segment->aStart + segment->length); i+=1)
{
// First populate array with random numbers between 1-100
segment->dataArray[i] = (1+ rand() % 100);

// Divide and multiply each element of the array by 100 random numbers upto 5
for (int j = 0; j < 2; j++)
{
segment->dataArray[i] = (segment->dataArray[i]*(2+rand() % 10));
}
}
}

int main()
{
int* dArray;
dArray = new int[ARRAYSIZE];
int segmentSize;

// Time Variables
timeval start, stop, result;
float t;

srand((unsigned)time(NULL));

cout << "Processing and Thread Testing with an Array of Random Numbers" << endl;

// Thread setup
for(int i = 1; i <= THREADS; i+=1)
{
// Thread Data Setup
pthread_t* thread = new pthread_t[THREADS];
arrayStruct* segment = new arrayStruct[THREADS];
segmentSize = ARRAYSIZE/THREADS;
void* exitStatus;

for(int j = 0; j < THREADS; j+=1)
{
pthread_t nThread;
arrayStruct nSegment;

// Array segment setup
nSegment.dataArray = dArray;
nSegment.aStart = (j * segmentSize);
nSegment.length = segmentSize;

thread[j] = nThread;
segment[j] = nSegment;
}

// Create thread
for(int j = 0; j < THREADS; j+=1)
pthread_create(&thread[j], NULL, thread_function, &segment[j]);

// Wait for thread to terminate
for(int j = 0; j < THREADS; j+=1)
pthread_join(thread[j], &exitStatus);

delete thread;
delete segment;
}

/*//Gets the time at the start of the test
gettimeofday(&start, NULL);


//Gets the time at the end of the test
gettimeofday(&stop, NULL);

// Timing calcuations and outputs
timersub(&stop, &start, &result);

t += (result.tv_sec + result.tv_usec/1000000.0);

cout << t;*/



return 0;
}


The issues I get are within the thread function:
declaration of 'arrayStruct* segment' shadows a parameter

and in main()
expected type-specifier before 'dataArray'

EDIT: Two original errors corrected. Stuck with a ';' issue. These issues drive me insane -_-

EDIT2: All errors fixed but now I have a new issue. When I increase threads it just increases the amount of elements. Or arrays.

So if ARRAYSIZE = 10 and THREADS = 2 I'll have 20 results post.
Last edited on
Solved the shadows a parameter issue. I was using the same variable name.

Type specifier issues also solved: Missed out 'int'

I will make the changes to the post but I am still missing a ';' before dataArray on line 40 which is near the start of main.
The error message is not the happiest one (you ought to post it, by the way)
dArray = new int dataArray[ARRAYSIZE]; ¿why did you write `dataArray' there?

Edit: OK, you did post it. ¿But why leave out critical information like the line number?
Last edited on
No error messages. But like said in my EDIT2: If I have 2 threads and 10 elements I'll have 20 results post out.
Topic archived. No new replies allowed.