Hello everyone,
disclaimer first - yes, this is a homework question. I didn't see a rule against posting those so I hope I'm in the clear.
So, my task is to translate a given code involving threads from C to C++. Afterwards I have to tinker with the code a bit, but for the moment that's irrelevant since I can't even get the translated code to work. Additionally, when I tried to run the untranslated C-code, I receive similiar error messages.
For reference, this is the code I was given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREADS 1
#define DATA_LENGTH 5000000
#define LEN_PER_THREAD DATA_LENGTH / THREADS
unsigned int data[DATA_LENGTH];
unsigned int sum(unsigned int nr){
unsigned int start, sum, i, j, d;
sum = 0;
start = nr * LEN_PER_THREAD;
printf("Start of thread %d\n", nr);
for(i = start; i < start + LEN_PER_THREAD; i++){
d = data[i];
for(j = 0; j < sizeof(unsigned int); j++){
if ((d%2) == 1)
++sum;
d >>= 1;
}
}
printf("End of thread %d\n", nr);
return sum;
}
void* work (void* arg){
return (void*) sum ((unsigned int) arg);
}
int main(){
unsigned int i, sum, ok, t;
pthread_t tid[THREADS];
void* ret;
for(i = 0; i < DATA_LENGTH; i++){
data[i] = random() % 10;
}
for(t = 0; t < THREADS; t++){
pthread_create(&tid[t], NULL, &work, (void*)t);
}
sum = 0;
for(t = 0; t < THREADS; t++){
pthread_join(tid[t], &ret);
sum += (unsigned int) ret;
}
printf("Die Berechnung ergibt: %d\n", sum);
return 0;
|
And this is the C++ version I created so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;
#define THREADS 1
#define DATA_LENGTH 5000000
#define LEN_PER_THREAD DATA_LENGTH / THREADS
unsigned int data[DATA_LENGTH];
unsigned int sum(unsigned int nr){
unsigned int start, sum, i, j, d;
sum = 0;
start = nr * LEN_PER_THREAD;
cout << "Start of thread " << nr << endl;
for (i = start; i < start + LEN_PER_THREAD; i++){
d = data[i];
for (j = 0; j < sizeof(unsigned int); j++){
if ((d%2) == 1){
++sum;
}
d >>= 1;
}
}
cout << "End of thread " << nr << endl;
return sum;
}
void* work (void* arg){
return (void*) sum ((unsigned int) arg);
}
int main(){
unsigned int i, sum, ok, t;
thread tid[THREADS];
void* ret;
for (i = 0; i < DATA_LENGTH; i++){
data[i] = rand() % 10;
}
for (t = 0; t < THREADS; t++){
tid[t](work, NULL);
}
sum = 0;
for (t = 0; t < THREADS; t++){
tid.join();
sum += (unsigned int) ret;
}
cout << "Die Berechnung ergibt: " << sum << endl;
return 0;
}
|
Further information: What I'm inferring from the code so far is that it's supposed to create multiple threads and keep them busy with an ultimately pointless task. Later on, I'm supposed to use <chrono> to compare runtimes after changing the globally defined THREADS. Also, the code is supposed to be faulty in a certain way leading to wrong results - it is however not supposed to not even compile obviously.
My big issue is understanding the syntax of the work-function and its result in the last two for-loops. I'm probably failing hard to translate the pthread-syntax as well, but that one also gives me errors when I try to compile the C-version that's supposed to be compilable.
I hope I included all relevant information. Of course I'll monitor this topic and try to answer asap. Thank you all for your help in advance!