Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I use to compute primes using linear code which took a long time. As I've an 8 cores processor I thought about multi-threading.

At first I use to put a huge number of int in a vector then share the vector between the threads which takeout each number which is not a prime. The first thread processed the 1st/nthread part, the second processed the 2nd/nthread and so on. Which leads me to a resource issue due to concurrent access.

Then I tried to split the vector in nthread vectors to give one vector to each thread, removing concurrent access issue. But I'm not convinced that it's the best way to do it since processing the primes from 1 to 10 000 is faster than processing primes form 10 001 to 20 000.

  • Is there a better way to take profit of the multi-threading ?
  • If no, what is the best way to split vectors so the thread would take the same time ?
share|improve this question
There's an excellent article here covering several techniques for multi-threading prime calculation. –  OldCurmudgeon Apr 2 at 8:58
@OldCurmudgeon I'll take a look and hope it'll fit with C++, thanks. –  Thomas Apr 2 at 9:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.