multithreading error ?

heya, uhh.. I think it happens while I am trying to push back item into the vector... (also this is one of my first few times trying out multithreading so I'm sorry if this code is not optimized and is a really bad code)

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <thread>
#include <functional>

void count_prime(int nStart, int nEnd, std::vector<uint64_t>& temp, int id);
int main()
{
    int nPrime;
    std::cin >> nPrime;
    std::vector<uint64_t> prime_batch_1, prime_batch_2;
    int n_prime_1, n_prime_2;
    if (nPrime % 2 == 0) //checks if nPrime is even
    {
        n_prime_1 = nPrime / 2;
        n_prime_2 = nPrime / 2;
    }
    else //if nPrime is not even
    {
        n_prime_1 = nPrime / 2;
        n_prime_2 = (nPrime / 2) + 1;
    }

    std::thread first_part( count_prime, 2, n_prime_1, std::ref( prime_batch_1 ) , 1 );
    std::thread second_part( count_prime, n_prime_2, nPrime, std::ref( prime_batch_2 ), 2 );

    first_part.join();
    second_part.join();

    for (int i = 0; i < prime_batch_1.size(); ++i)
    {
        std::cout << prime_batch_1.at( i );
    }
    for (int i = 0; i < prime_batch_2.size(); ++i)
    {
        std::cout << prime_batch_2.at( i );
    }
}

void count_prime(int nStart, int nEnd, std::vector<uint64_t>& temp, int id)
{
    bool isPrime;
    for (int i = nStart; i <= nEnd; ++i)
    {
        std::cout << "hello from thread : " << id << " at : " << i << "\n";
        isPrime = true;
        if (i == 2);
        if (i % 2 == 0)
        {
            isPrime = false;
            goto end_label;
        }
        else
        {
            for (int n = 0; n <= i / 2; ++n)
            {
                if (i % n == 0)
                {
                    isPrime = false;20
                    goto end_label;
                }
            }
        }
        temp.push_back(i);
        end_label :;
    }
}


I am using codeblocks and MinGW 6.1.0
Flaze07 wrote:
I think it happens
what happens? run-time crash?
Not gonna lie, I got distracted by the refs and it appears the count_prime method isn't working properly (try it without threads).

If you want output from an asynchronous task, you should probably use future, which will eventually give you output in a package known as a promise. Example closely based on the various pages at http://en.cppreference.com/w/cpp/thread/future , http://en.cppreference.com/w/cpp/thread/future/wait

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
#include <cmath>
#include <iostream>
#include <vector>
#include <future>

using namespace std;

vector<bool> Sieve(unsigned int n)
{
    vector<bool> v(n+1, true);
    v[0] = v[1] = false;
    for (int i=2; i<sqrt(n); ++i)
    {
        if (v[i])
        {
            for (int j=i*i; j<=n; j+=i)
                v[j] = false;
        }
    }
    return v;
}

void Print(const vector<bool>& v)
{
    for (int i=0; i<v.size(); ++i)
        if (v[i])
            cout << i << " ";
    cout << endl;
}

int main()
{
    auto f1 = async(Sieve, 100);
    auto f2 = async(Sieve, 50);
    cout << "waiting...\n";
    f1.wait();
    f2.wait();
    
    auto primes = f1.get();
    auto primes2 = f2.get();
    Print(primes);
    Print(primes2);
    cout << endl;
}


Running at https://repl.it/repls/RelevantSnarlingGenericsoftware
waiting...
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
Last edited on
yeah, it just...stops. and the "do you want to debug the program window pops up"
The second operand to % is not allowed to be zero (just like with /) but this could happen on line 57.
Topic archived. No new replies allowed.