2

I am trying to make a program that uses a very inefficient algorithm that computes perfect numbers within a range using POSIX threads. I can't seem to grasp the concept of locking well enough to get my algorithm to work correctly. I want to return a list of perfect numbers. Can anyone offer some advice on how to implement this better?

Specific Questions: - How do I make it print out only 1 instant of each perfect number? - How do I make it return the value instead of just print the values?

Source:

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

static void * threadFunc(void *arg) {

    int range, start, end;
    int i, s,  number, mod, divisor, temp, sum;

    s = pthread_mutex_lock(&mtx);   

    /* Takes in a string and pulls out the two integers */
    sscanf(arg,"%i-%i", &start, &end);
    printf("\nStart: %i\nEnd: %i\n", start, end);

    printf("\n");

    s = pthread_mutex_unlock(&mtx);

    for (number=start; number<=end; number++) { // loop through range of numbers                

        temp=0,sum=0;           
        // loops through divisors of a number and sums up whole divisors
        for (i=1; i<number; i++) {          
            //s = pthread_mutex_lock(&mtx);             
            mod = number % i;           
            //s = pthread_mutex_unlock(&mtx);           

            if (mod == 0){              
                s = pthread_mutex_lock(&mtx);               

                divisor = i; 
                sum = divisor + temp;
                temp = sum;

                s = pthread_mutex_unlock(&mtx);                     
            }                       
        }
        //if the sum of whole divisors is equal to the number, its perfect
        if (sum == number)  {           

            s = pthread_mutex_lock(&mtx);           

            printf("%i is a Perfect Number \n", sum);
            //return sum somehow;           

            s = pthread_mutex_unlock(&mtx);         
        }
    }

    return NULL;
}


int main(int argc, char *argv[]) {
    pthread_t tid[5];

    int prefect_number, i, s;

    char input[]="1-9999";

    for(i=0; i < 5; ++i) {
        pthread_create(&tid[i], NULL, &threadFunc, input);
        print_thread_info();
    }
    /* Wait for the perfect number thread to complete, then get result. */  
    for(i=0; i < 5; ++i)
        pthread_join(tid[i],NULL);

    return 0;   
}
false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

0

You only need to lock a mutex when accessing a data structure (e.g. global variable) or resource (e.g. the terminal) that might be modified from another thread.

  • You don't need to lock a mutex for accessing variables local to a single thread
  • You don't need to lock a mutex for reading global variables that are never modified by another thread

So in your case the single use case for a mutex would be to prevent multiple printf's() getting their outputs possibly mixed up.

Also, it's pointless to have all threads explore the same range. Perhaps you want 5 threads exploring different ranges each (0-1999, 2000-2999 ... 8000-9999)?

pthread_join() returns the exit value of the thread (the one you pass to return or to pthread_exit()). If you want to return several values, you may want to create a global variable holding a linked list of numbers (which would, finally, need a mutex, since different threads would need to write to it)

dan3
  • 2,528
  • 22
  • 20