C++11 Threads

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!
Last edited on
yes, this is a homework question. I didn't see a rule against posting those so I hope I'm in the clear.

http://www.cplusplus.com/forum/beginner/1/
Welcome -- read before posting!
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.

Hi, Snacks.
I‘ve no problem in admitting I can’t understand your code. Actually, I haven’t studied threads yet.
Anyway, from my point of view, if you give your variables the same names of your functions and than pass them to your thread constructor, you’re making your life harder than needed.
And about void pointers... I haven’t seen one of them for ages... Do not use them in C++, there are far better alternatives.

The following code compiles, but I’m sure it doesn’t perform what you wanted it to do - however it compiles: it’s a start, isn’t it? :-)

I hope someone else come here to give you a cannier answer than mine. Good luck!

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
#include <functional>
#include <iostream>
#include <thread>
#include <vector>
 
constexpr int THREADS = 1;
constexpr int DATA_LENGTH = 5; // 5'000'000;
constexpr int LEN_PER_THREAD = DATA_LENGTH / THREADS;
 
unsigned int data[DATA_LENGTH]; 
 
unsigned int sum(unsigned int nr)
{
    unsigned sum = 0;
    unsigned start = nr * LEN_PER_THREAD;
    std::cout << "Start of thread " << nr << '\n';
    for (unsigned i = start; i < start + LEN_PER_THREAD; i++) {
        unsigned d = data[i];
        for (unsigned j = 0; j < sizeof(unsigned int); j++) {
            if ((d%2) == 1) { ++sum; }
            d >>= 1;
        }
    }
    std::cout << "End of thread " << nr << '\n';
    return sum;
}
 
int main()
{
    std::vector<std::thread> tid;
    for (unsigned i = 0; i < DATA_LENGTH; i++) { data[i] = rand() % 10;  }
    unsigned sum_var = 1;
    for (unsigned t = 0; t < THREADS; t++) { tid.emplace_back(sum, std::ref(sum_var)); }  
    for (unsigned t = 0; t < THREADS; t++) { tid.at(t).join(); }
    std::cout << "Die Berechnung ergibt: " << sum_var << '\n';
    return 0;
}

Topic archived. No new replies allowed.