calculate prme numbers by threads

I have to calculate prime numbers between ranges by threads.
example program run


./myProg 100 250 4 //4 number of threads and 100 to 250 is range


I did basic part, what is the next step?

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<pthread.h>
#include<cstring>
#include<cstdlib>

using namespace std;


void* prime(void*);//calculate prime number for this thread between range x to y ???
pthread_mutex_t mutx=PTHREAD_MUTEX_INITIALIZER;
int total_count=0;
int lower_bound_=0;

int main(int argc, char** argv){

    int assigned_for_each_t=0;

    if(argc!=4){
        cout<<"Must provide exactly 3 arguments"<<endl;
        exit(EXIT_FAILURE);
        }

    pthread_t threads[(*argv[3])];


    cout<<"Prime numbers will be calculated by "<<(argv[3])<<" threads"<<endl;

    //assign numbers to threads 1/n(b-a+1)
    int num_gt=atoi(argv[2]), num_lw=atoi(argv[1]), denom=atoi(argv[3]);

    assigned_for_each_t=((num_gt-num_lw)/denom);
    lower_bound_=num_lw;

    cout<<"Every thread assigned "<<assigned_for_each_t<<endl;

    for(int thr_=1; thr_<=denom;thr_++){
        if(thr_){
            bool is_prime;
            //pthread_mutex_lock(&mutx);
            int prime_in_thread=0;
            pthread_create(&threads[thr_],NULL, prime, (void *)&lower_bound_);
            pthread_mutex_lock(&mutx);

            for(int i=lower_bound_; i<(lower_bound_+lower_bound_); i++){
                is_prime=true;
                for(int j=2;j<10;j++){

                    if(i%j==0){

                        is_prime=false;


                        }

                    }
                    if(is_prime==true)
                        prime_in_thread=prime_in_thread+1;

                }

                    total_count=total_count+prime_in_thread;

                    cout<<"Total prime numbers in thread "<<thr_<<" "<<prime_in_thread<<endl;;
                    pthread_mutex_unlock(&mutx);

                    lower_bound_=assigned_for_each_t+lower_bound_;



            }
    }
    cout<<endl;
    cout<<"Total prime numbers "<<total_count<<endl;
    return 0;
}

void* prime (void* lower_bound_){


    int prime_in_thread=0;

    int* lower_bound__=(int*)lower_bound_;

    for(int i=*lower_bound__; i<(*lower_bound__+*lower_bound__); i++){
        bool is_prime=true;
        for(int j=2;j<10;j++){

            if(i%j==0){


               is_prime=false;

            }
            if(is_prime==true)
                 prime_in_thread=prime_in_thread+1;

        }

    }

    total_count=total_count+prime_in_thread;

    pthread_exit(NULL);



}


Last edited on
There are lots of problems, little technical ones like what does this do:
 
    pthread_t threads[(*argv[3])];

to design ones like, what is your algorithm and what does each thread do?


Let's start with the algorithm. Can you describe in words what your algorithm is and how threads improve it?
Last edited on
Topic archived. No new replies allowed.