2016-04-08 40 views
1

我試圖計算從X到Y的所有素數,並將不同PThread之間的計算分開。計算素數PThread

所以,例如:

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

3分之36= 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); 
     } 
    } 
} 

我如何可以實現此得到預期的輸出任何線索?

+2

你沒有顯示你得到的結果而不是理想的結果或解釋了什麼問題。 –

+0

@SamiKuhmonen嗯,我只是有點困惑於如何多線程計算從1號到2號素數的計算。我想我已經把它設置正確,但是,我的良心告訴我這是錯誤的。 – Anon

+1

您所有的線程正在計算'NUM1'和'NUM2'之間的區間中的所有素數。如果你想限制它們到一個較小的時間間隔,你需要把這個時間間隔傳遞給線程函數。 – molbdnilo

回答

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

    } 

} 
+0

看到第6行,NUM2應該是40.在第25到第28行中,我添加了計數器來更新數字的範圍,並在第60行添加了一個pthread_join調用來等待執行第一個線程來調用另一個線程。 – kamlesh