Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:07:02+00:00 2026-05-30T15:07:02+00:00

I’m making a prime number sieve using Eratosthenes Sieve in C++ using multiple threads,

  • 0

I’m making a prime number sieve using Eratosthenes Sieve in C++ using multiple threads, but when I use more than one thread, the results are inconsistent. The range it has to be able to go through is 1-2^32. When run with a smaller range, like 1-1024, it comes up with the right number of primes usually, but as the range gets bigger, so does the margin of error. I’m assuming it’s a race condition, since I don’t use mutexes (and would prefer not to, since it shouldn’t be necessary with how the program is set up), or there’s something wrong with how I’m sending data to the thread function. I’m still getting used to C++ and it’s pointers/referencing. The number of primes it finds is always greater than or equal to the actual number, never less than. The index of the bit in a bitmap gets set to 1 for composite numbers. It’s probably just some stupid little bug I’m overlooking from lack of C/C++ experience. Please let me know if I’ve been unclear about anything. Thanks for looking.

#include <iostream>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sched.h>
#include <vector>
#include <math.h>

using namespace std;

#define NUM_OF_THREADS 4
#define UPPER_LIMIT pow(2, 30)
#define SQRT_UPPER_LIMIT sqrt(UPPER_LIMIT)

typedef struct {
    int thread_index;
    unsigned long long prime;
    unsigned long long marked_up_to;
    pthread_t thread;
    bool thread_is_done;
} thread_info_t;

typedef struct {
    unsigned long long x;
    unsigned long long y;
}indexes_t;

static const unsigned long bitmasks[] = {
    0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000,
    0x800000000000000, 0x400000000000000, 0x200000000000000, 0x100000000000000,
    0x80000000000000, 0x40000000000000, 0x20000000000000, 0x10000000000000,
    0x8000000000000, 0x4000000000000, 0x2000000000000, 0x1000000000000,
    0x800000000000, 0x400000000000, 0x200000000000, 0x100000000000,
    0x80000000000, 0x40000000000, 0x20000000000, 0x10000000000,
    0x8000000000, 0x4000000000, 0x2000000000, 0x1000000000,
    0x800000000, 0x400000000, 0x200000000, 0x100000000,
    0x80000000, 0x40000000, 0x20000000, 0x10000000,
    0x8000000, 0x4000000, 0x2000000, 0x1000000,
    0x800000, 0x400000, 0x200000, 0x100000,
    0x80000, 0x40000, 0x20000, 0x10000,
    0x8000, 0x4000, 0x2000, 0x1000,
    0x800, 0x400, 0x200, 0x100,
    0x80, 0x40, 0x20, 0x10,
    0x8, 0x4, 0x2, 0x1
};
clock_t start;
clock_t stop;
static unsigned long *bitmap; //array of longs
static int bits_in_element = sizeof(unsigned long long)*8;
static thread_info_t info[NUM_OF_THREADS];

indexes_t bit_indexes_from_number(unsigned long long number);
void print_bitmap();
bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j);


static void * threadFunc(void *arg)
{   
    thread_info_t *thread_info = (thread_info_t *)arg;

    unsigned long long prime = thread_info->prime;
    unsigned long long comp_number = prime+prime;
    int thread_index = thread_info->thread_index;
    indexes_t comp_index;

    for(; comp_number <= UPPER_LIMIT; comp_number += prime) // get rid of prime multiples
    {
        comp_index = bit_indexes_from_number(comp_number);
        bitmap[comp_index.x] |= bitmasks[comp_index.y];
        info[thread_index].marked_up_to = comp_number; // so main thread only checks for primes past what's been marked
    }

    thread_info->thread_is_done = true;

    return NULL;
}


int main (int argc, char * argv[])
{
    long ncpus;
    double total_time;
    unsigned long long num_to_check;
    thread_info_t *thread_to_use;
    int thread_ret_val;

    start = clock();

    for(int i = 0; i < NUM_OF_THREADS; i++)
    {
        info[i].thread_index = i;
        info[i].marked_up_to = 2;
        info[i].thread_is_done = true;
    }

    for(int i = 0; i < NUM_OF_THREADS; i++)
    {
        bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8);
        bitmap[0] |= (bitmasks[0] | bitmasks[1]);
    }

    for(unsigned long long i = 0; i <= SQRT_UPPER_LIMIT/bits_in_element; i++)// go thru elements in array
    {
        for(unsigned long long j = (i == 0 ? 2 : 0); j < bits_in_element; j++) //go thru bits in elements
        {
            num_to_check = (i * bits_in_element) + j;

            //make sure all threads are past num_to_check
            for(int k = 0; ; k++)
            {
                if(k == NUM_OF_THREADS)
                    k = 0;
                if(info[k].marked_up_to >= num_to_check)
                    break;
            }

            if(check_if_bit_index_is_prime(i, j)) //check if bit index is prime
            {
                for(int k = 0; ; k++) //wait for a finished thread to use
                {
                    if(k == NUM_OF_THREADS)
                        k = 0;
                    if(info[k].thread_is_done)
                    {
                        thread_to_use = &info[k];
                        info[k].thread_is_done = false;
                        info[k].prime = (i * bits_in_element) + j;
                        break;
                    }
                }

                thread_ret_val = pthread_create(&thread_to_use->thread, NULL, threadFunc, (void *)thread_to_use); //thread gets rid of multiples
                if(thread_ret_val != 0)
                {
                    cerr << "thread error: " << strerror(thread_ret_val) << "\n";
                    return -1;
                }
            }
        }
    }

    for(int i = 0; i < NUM_OF_THREADS; i++)
    {
        printf("waiting on %d\n", i);
        thread_ret_val = pthread_join(info[i].thread, NULL);
        if(thread_ret_val != 0)
        {
            cout << strerror(thread_ret_val);
        }
    }

    stop = clock();

    total_time = (double)(stop - start) / (double)CLOCKS_PER_SEC;

    ncpus = sysconf(_SC_NPROCESSORS_ONLN);

    /* Print performance results */
    printf ("Total time using %d threads : %.6f seconds\n",
            NUM_OF_THREADS, total_time / (NUM_OF_THREADS < ncpus ? NUM_OF_THREADS : ncpus));

    print_bitmap();

    return 1;
}

indexes_t bit_indexes_from_number(unsigned long long number)
{
    indexes_t indexes;

    indexes.x = ceill(number / bits_in_element); //ceiling or not??

    if(indexes.x == 0)
        indexes.y = number;
    else
        indexes.y = number - indexes.x*bits_in_element;

    return indexes;
}


void print_bitmap()
{
    int count = 0;

    for(int i = 0; i <= UPPER_LIMIT; i++)
    {
        if(check_if_bit_index_is_prime(bit_indexes_from_number(i).x, bit_indexes_from_number(i).y))
        {
            count++;
        }
    }
    cout << "number of primes between 1 and " << UPPER_LIMIT << ": " << count << "\n";
}


bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j)
{
    if(bitmap[i] & bitmasks[j])
        return false;
    else
        return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:07:03+00:00Added an answer on May 30, 2026 at 3:07 pm

    The major major issue is your thread_info_t. You need to ensure atomicity of the changing of members in this structure. The fact that this is not atomic is what is most likely causing your issues of too many primes. Ensuring that these operations are atomic is either going to rely on c++11’s std::atomic or platform specific details. Of course you could use locks instead to make sure that only one thread touches a thread_info_t at a time.


    There is also some problems with the following code:

     bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8);
     bitmap[0] |= (bitmasks[0] | bitmasks[1]);
    

    There are two errors in this piece of code. First, there is a memory leak, every allocation except the last is lost, with no pointers pointing to it and no freeing possible.

    Second, after allocating the memory for bitmap, the data is undefined. Thus bitmap[0] |= (bitmasks[0] | bitmasks[1]); is invalid as bitmap[0] has an undefined value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.