apparenty the OP got their answer already, in the comments, and the issue is resolved now.
I have coded a prime number program (sieve of eratosthenes) that executes using pthreads.
This is my first multithreading program and I don't know why my program takes roughly 3 mins. time to execute. Thats too much time!
Can someone tell me where exactly am I wrong:
#include<iostream>
#include<cstring>
#include<pthread.h>
#include<time.h>
using namespace std;
//set limits
#define LIMIT 100000001
#define THREAD_LIMIT 8
//declare buffers
bool num[LIMIT];
unsigned long long num_of_prime = 1; // 2 is counted as prime initially
unsigned long long sum_prime = 2; // 2 is counted in sum of primes
void *search(void *);
int main()
{
clock_t start_time = clock(); // start clock stamp
pthread_t thread[THREAD_LIMIT];
int thread_val=-1,j=-1;
unsigned long long i=3;
bool *max_prime[10]; // stores max. 10 prime numbers
memset(num,0,LIMIT); // initialize buffer with 0
while(i<LIMIT)
{
if(num[i]==0)
{
num_of_prime++;
sum_prime +=i;
j = ++j%10;
max_prime[j]=num+i;
thread_val=++thread_val%THREAD_LIMIT;
pthread_join(thread[thread_val],NULL); // wait till the current thread ends
pthread_create(&thread[thread_val],NULL,search,(void *)i); // fork thread function to flag composite numbers
}
i+=2; // only odd numbers
}
// end all threads
for(i=0;i<THREAD_LIMIT;i++)
{
pthread_join(thread[i],NULL);
}
cout<<"Execution time: "<<((double)(clock() - start_time))/CLOCKS_PER_SEC<<"\n";
cout<<"Number of Primes: "<<num_of_prime<<"\n";
cout<<"Sum of Primes: "<<sum_prime<<"\n";
cout<<"List of 10 Max. Primes: "<<"\n";
for(i=0;i<10;i++)
{
j=++j%10;
cout<<(max_prime[j]-num)<<"\n";
}
return 0;
}
void *search(void *n)
{
unsigned long long jump = (unsigned long long int)n;
unsigned long long position = jump*jump; // Jump to N*N th comppsite number
bool *posn = num;
jump<<=1;
while(position<LIMIT)
{
(*(posn+position))?(position+=jump):(*(posn+position)=1,position+=jump);
}
return NULL;
}
Contraints: Only 8 threads can be forked.
N: 10^8
How can I improve the efficiency of this code (especially in forking & joining the threads)?