计算素数PThread

时间:2016-04-08 07:31:19

标签: c multithreading

我试图计算从X到Y的所有素数,并在不同的PThread之间拆分计算。

所以,例如:

NUM1 = 4
NUM2 = 40
NUMOFTHREADS = 3
NUM2 - NUM1 = 36

36/3 = 12,因此每个线程将进行12次计算。线程1从4到16,线程2从16到28,线程3从28到36.然后将结果加起来。

我已经在网上阅读了大量的例子,但是,他们似乎没什么帮助,所以我为PThreads实现了自己版本的主要检查程序。

预期产出:

Thread 1 calculated: 5, 7, 11, 13
Thread 2 calculated: 17, 19, 23
Thread 3 calculated: 29, 31, 37

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define NUM1    4
#define NUM2    80
#define NUMOFTHREADS    3

/* Prototypes */
void *printPrimeThreadCalculation(void *threadid);
void primeCheckerThread(int num1, int num2, int numOfThreads);

int main() {

    /* Thread Prime Checker */
    primeCheckerThread(NUM1, NUM2, NUMOFTHREADS);
    /* Exits the threads */
    pthread_exit(NULL);
    return 0;
}

void *printPrimeThreadCalculation(void *threadid) {
    long tid;
    tid = (long) threadid;

    int number1 = NUM1;
    int number2 = NUM2;
    int isPrime, i;

    /* Calculations */
    while (number1 <= number2) {
        /* Assume isPrime is true */
        isPrime = 1;

        for (i = 2; i < number1 && isPrime; i++) {
            if (number1 % i == 0) {
                isPrime = 0;
            }
        }
        if (isPrime == 1) {
            printf("%d ", number1);
        }
        number1++;
    }
    printf("\n");
}

void primeCheckerThread(int num1, int num2, int numOfThreads) {
    /* Create threads */
    pthread_t threads[numOfThreads];
    int rc;
    long t;
    for (t = 0; t < numOfThreads; t++) {
        /* Creates threads */
        rc = pthread_create(&threads[t], NULL, printPrimeThreadCalculation, (void *)t);
        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
}

有关如何实现此目的以获得预期输出的任何线索?

1 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define NUM1    4
#define NUM2    40
#define NUMOFTHREADS    3

/* Prototypes */
void *printPrimeThreadCalculation(void *threadid);
void primeCheckerThread(int num1, int num2, int numOfThreads);

int main() {

    /* Thread Prime Checker */
    primeCheckerThread(NUM1, NUM2, NUMOFTHREADS);
    /* Exits the threads */
    pthread_exit(NULL);
    return 0;
}

void *printPrimeThreadCalculation(void *threadid) {
    long tid;
    tid = (long) threadid;
    static int cnt=0; //cnt=count
    int number1 = NUM1+(NUM2-NUM1)*cnt/NUMOFTHREADS;
    int number2 = NUM1+(NUM2-NUM1)*(cnt+1)/NUMOFTHREADS;
    cnt+=1;
    int isPrime, i;

    /* Calculations */
    while (number1 <= number2) {
        /* Assume isPrime is true */
        isPrime = 1;

        for (i = 2; i < number1 && isPrime; i++) {
            if (number1 % i == 0) {
                isPrime = 0;
            }
        }
        if (isPrime == 1) {
            printf("%d ", number1);
        }
        number1++;
    }
    printf("\n");
}

void primeCheckerThread(int num1, int num2, int numOfThreads) {
    /* Create threads */
    pthread_t threads[numOfThreads];
    int rc;
    long t;
    for (t = 0; t < numOfThreads; t++) {
        /* Creates threads */
        rc = pthread_create(&threads[t], NULL, printPrimeThreadCalculation, (void *)t);
        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }  pthread_join(threads[t],NULL);

    }

}