I was looking at this question and I was wondering how I can convert this program into a thread pool.
Here, I like to call twine_range
at each thread. However, it requires arguments. In addition, this function affects the global variable twine_primes
which is at the risk of multiple access.
I also don't care about the order of the results.
Any <thread>
or <boost/thread.hpp>
based solution is welcome.
#include <string>
#include <iostream>
#include <vector>
std::vector<long> twine_primes;
bool is_prime(long a)
{
if(a<2l)
return false;
if(a==2l)
return true;
for(long i=2;i*i<=a;i++)
if(a%i==0)
return false;
return true;
}
void twine_range(long l1,long l2)
{
for(long l=l1;l<=l2;l++)
if(is_prime(l) && is_prime(l+2))
twine_primes.push_back(l);
}
int main()
{
twine_range(10000000l,20000000l);
for(long l:twine_primes)
{
std::cout
<<l<<" and "<<(l+2)
<<" are twine prime numbers."
<< std::endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire