多线程完美数字

时间:2018-11-30 17:25:22

标签: c

我创建了一个多线程程序来检查数字是否为完美数字。我在local_sum中使用了void *myThreadTest(void *vargp)变量,并在该函数中打印出一个数字因子。无论如何,是否要在main中而不是void *myThreadTest(void *vargp)中打印数字因子?我的教授要我在main内打印所有内容,因此我需要解决此问题。

这是我的代码:

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct thread_data{
    unsigned long long start_nr, end_nr;
};

struct shared_data{
    unsigned long long nr_of_thread;
    unsigned long long input;
    struct thread_data *td;
    unsigned long long sum;
};

struct shared_data sd;

pthread_mutex_t mutexsum;

void split_nr(unsigned long long nr_of_thread, unsigned long long input, struct thread_data *td){
    // need to div to [1 -> n/2] numbers
    unsigned long long nr_need_to_check = input/2;

    // initial start number of each threads
    for (int i=0; i<nr_of_thread; i++){
        td[i].start_nr = nr_need_to_check/nr_of_thread * i + 1;
    }

    int res = nr_need_to_check % nr_of_thread;
   // printf("res = %i\n", res);

    int adding = 2;
    if (nr_need_to_check % 2 == 1){
        adding = 1;
    }

    // update start number of each threads and compute its end numbers
    for (int i=nr_of_thread-res+1; i<nr_of_thread; i++){
        td[i].start_nr = td[i].start_nr + i-adding;
    }
    // update end numbers of each threads
    for (int i=0; i<nr_of_thread-1; i++){
        td[i].end_nr = td[i+1].start_nr - 1;
    }

    td[nr_of_thread-1].end_nr = nr_need_to_check;
}

void *myThreadTest(void *vargp){

    struct thread_data *inp = (struct thread_data *) vargp;

   // printf("from %llu -> %llu\n", (*inp).start_nr, (*inp).end_nr);

    // if any number in this range is input's factor ..
    unsigned long long *local_sum = 0;
    for (unsigned long long i=(*inp).start_nr; i<(*inp).end_nr+1; i++){
            if (sd.input % i == 0) {
                printf("%llu \n",i);
                local_sum = local_sum + i; // .. then add to local_sum
            }
    }

    //update global sum
    pthread_mutex_lock (&mutexsum);
    sd.sum += local_sum;
    pthread_mutex_unlock (&mutexsum);
    pthread_exit((void*) 0);

}

int main(int argc, char **argv)
{
    if (argc != 3) {
        printf("Usage: %s <positive number> <counts per thread>\n", argv[0]);
        return 1;
    }

    char *ptr;
    unsigned long long input = strtoull(argv[1], &ptr, 10);
    unsigned long long nr_of_thread = atoi(argv[2]);

    if (nr_of_thread < 1) {
        printf("The thread count must be at least 1\n");
        return 2;
    }

    struct thread_data td[nr_of_thread];

    // split input/2 +1 into smaller interval to check factors
    split_nr(nr_of_thread, input, td);
    printf("Factors of %llu is \n", input);

    sd.input = input;
    sd.nr_of_thread = nr_of_thread;
    sd.td = td;
    sd.sum = 0;

    pthread_t threads[nr_of_thread];

    pthread_mutex_init(&mutexsum, NULL);

    for (unsigned long long i=0; i<nr_of_thread; i++){
       // printf("create thread %i ...\n", i);
        pthread_create(&threads[i], NULL, myThreadTest, &sd.td[i]);
    }

    for (unsigned long long  i=0; i<nr_of_thread; i++){
       // printf("join %i ...\n", i);
        pthread_join(threads[i], NULL);
    }

    printf("Sum of factors is:  %llu\n\n", sd.sum);

    if (sd.sum == sd.input && sd.sum != 0){
        printf("%llu is a perfect number\n", sd.input);
    }
    else {
        printf("%llu is not perfect number\n", sd.input);
    }

    pthread_mutex_destroy(&mutexsum);
    pthread_exit(NULL);

    return 0;

}

0 个答案:

没有答案