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

  • SEARCH
  • Home
  • 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 7529923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:46:12+00:00 2026-05-30T04:46:12+00:00

I’m writing a prime sieve program using eratosthenes’ for class. I have 8 threads

  • 0

I’m writing a prime sieve program using eratosthenes’ for class. I have 8 threads going, each one responsible for a segment of the number range [1, 2^32]. For some reason, sometimes thread[0] in my threads array doesn’t make it to the function that the thread goes to when it’s created. The other ones always (it seems) do. Please let me know what’s wrong with this code. A warning though, I’m just learning C++, so there might be syntax errors, etc., which I assume is causing the bug. Been spending hours on this and narrowed it down to the thread[0] not always making it to the function. I changed the values of the #define’s so it would be easier to debug. Bug happens either way. Please, I’m not too interested in other comments on how I could improve the program. It’s due soon, so I just want to get it working as it is. Thanks so much!

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

using namespace std;

#define NUM_OF_THREADS 8
#define TWO_TO_THIRTY_SECOND 1000000 //4294967296
#define SQRT_TWO_TO_THE_THIRTY_SECOND 1000 //65536
#define TWO_T0_THIRTY_SECOND_OVER_EIGHT 125000 //536870976

typedef struct {
    unsigned long composite_to_remove_index;
    int thread_index;
    unsigned long prime_number;
} thread_info_t;

bitset<TWO_T0_THIRTY_SECOND_OVER_EIGHT> bitmap[NUM_OF_THREADS];
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_t thread[NUM_OF_THREADS];


static void * threadFunc(void *arg)
{

    thread_info_t info = *(thread_info_t *)arg;

    bitmap[info.thread_index][info.composite_to_remove_index] = 1;
    info.composite_to_remove_index += info.prime_number;

    int index_to_remove = (int)info.composite_to_remove_index;

    for(; index_to_remove < TWO_T0_THIRTY_SECOND_OVER_EIGHT; index_to_remove += info.prime_number)
    {
        if(bitmap[info.thread_index][index_to_remove] == 0)
        {
            bitmap[info.thread_index][index_to_remove] = 1;
            if(info.thread_index == 0)
            {
                cout << "bit " << index_to_remove << ": " << bitmap[info.thread_index][index_to_remove] << "\n";
            }
        }
    }

    return NULL;
}

int main (int argc, char * argv[])
{   
    int thread_ret_val;
    vector<unsigned long long> prime_numbers;
    thread_info_t info; 

    for(unsigned long long i = 2; i < SQRT_TWO_TO_THE_THIRTY_SECOND; i++)
    {
        if(bitmap[0][i] == 0)
        {
            prime_numbers.push_back(i);
            info.prime_number = i;

            for(unsigned long j = 0; j < NUM_OF_THREADS; j++)
            {
                if(j == 0)
                    info.composite_to_remove_index = i*2;
                else
                    info.composite_to_remove_index = (((TWO_TO_THIRTY_SECOND/NUM_OF_THREADS)*j) % i);
                info.thread_index = (int)j;


                thread_ret_val = pthread_create(&thread[info.thread_index], NULL, threadFunc, (void*)&info);
                if(thread_ret_val != 0)
                {
                    cerr << "create thread error " << strerror(thread_ret_val) << "\n";
                }
            }

            for(int j = 0; j < NUM_OF_THREADS; j++)
            {
                pthread_join(thread[j], NULL);
            }
        }
    }

    return 1;
}
  • 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-30T04:46:13+00:00Added an answer on May 30, 2026 at 4:46 am

    info structure that is passed to all threads is meant to be unique for each thread but is here inadvertently and occasionally shared between threads. The problem is that info is not guaranteed to be unchanged between the call to pthread_create and the beginning of threadFunc. After calling pthread_create you change the contents of info in next iteration, and it may happen that only then the previous thread actually copies info in the first line of threadFunc.

    You should have std::vector of info structures, and call pthread_create every time with different info.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
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 have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.