Marty
Marty

Reputation: 6184

Why is this code inconsistently crashing?

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;
}

Upvotes: 0

Views: 165

Answers (1)

Dialecticus
Dialecticus

Reputation: 16761

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.

Upvotes: 1

Related Questions