为什么pthread_join()的返回值在每次调用pthread_join()

时间:2016-02-21 04:00:24

标签: c multithreading void-pointers prime-factoring pthread-join

如果用户在命令行中只输入1个数字,则此程序可以正常工作。它会将主要因素分解出来并将它们输出到控制台就好了。

J_10542741@cs3060:~/assn3$ ./assn3 12
12: 2, 2, 3,

我的问题是当我在其他两个案例中测试它时:

A)多个参数:

J_10542741@cs3060:~/assn3$ ./assn3 10 8 6
10: 2, 5,
8: 2, 5,
6: 2, 5,

B)使用一系列数字(即{1..5}):

J_10542741@cs3060:~/assn3$ ./assn3 {1..5}
1:
2:
3:
4:
5:

我已经浏览了这个网站,了解pthread_join()的返回值以及通过Google搜索的任何内容。我觉得这部分代码存在问题:

 // FOR loop to join each thread w/ Main Thread 1x1
    for(i = 0; i < argc-1; i++){
            retCode = pthread_join(t_id[i], &factors);
            results = factors;
            if (retCode != 0){
                    // Print Error Message and return -1
                    fprintf(stderr, "Failure to join threads.");
                    return -1;
            }
            else{
                    printf("%s: ", argv[i+1]);
                    while(*results != 0){
                            printf("%d, ", *results);
                            results++;
                    }
            }
            free(factors);
            printf("\n");
    }

以下是其中的代码:

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

// Return a pointer to the pFactors array
void *primeFactors(void* number){
    int *pFactors = malloc(sizeof(int));    // Dynamic Array for prime factors

    int capacity = 0;
    int size = 1;
    int num = atoi((char*)number);
    int prime = 2;

    // If num < 4, then that number is already prime
    if(num < 4)
            pFactors[capacity] = num;
    else{
            while(num > 1){
                    while(num % prime == 0){
                            if(capacity == size){
                                    size++;
                                    pFactors = realloc(pFactors, size*sizeof(int));
                            }
                            num /= prime;
                            pFactors[capacity] = prime;
                            capacity++;
                    }
                    prime++;
            }
    }
    if(capacity == size){
            size++;
            pFactors = realloc(pFactors, size*sizeof(int));
    }
    pFactors[capacity] = 0;
    pthread_exit((void*)pFactors);
}

// MAIN FUNCTION
int main(int argc, char* argv[]){
    int i, retCode;         // retCode holds the value of successful/fail operation for pthread_create/join
    int j = 1;

    int* results;
    void* factors;

    //Thread Identifier value is equal to the number of actual int(s) in argv
    pthread_t t_id[argc-1];

    // Check argc for too few arguments
    if(argc < 2){
            fprintf(stderr, "Usage: ./assn3 <integer value>...");
            return -1;
    }

    // Loop through argv and check argv[j] value to ensure it's >= 0
    while(j <= argc-1){
            if(atoi(argv[j]) < 0){
                    fprintf(stderr, "%d must be >= 0", atoi(argv[j]));
                    return -1;
            }
            j++;
    }

    // Create the thread
    for(i = 0; i < argc-1; i++){
            retCode = pthread_create(&t_id[i], NULL, primeFactors, *(argv+1));
            if (retCode != 0){
                    // Print Error Message and return -1
                    printf("Failure to start thread. Error: %d\n", retCode);
                    return -1;
            }
    }

    // FOR loop to join each thread w/ Main Thread 1x1
    for(i = 0; i < argc-1; i++){
            retCode = pthread_join(t_id[i], &factors);
            results = factors;
            if (retCode != 0){
                    // Print Error Message and return -1
                    fprintf(stderr, "Failure to join threads.");
                    return -1;
            }
            else{
                    printf("%s: ", argv[i+1]);
                    while(*results != 0){
                            printf("%d, ", *results);
                            results++;
                    }
            }
            free(factors);
            printf("\n");
    }
    return 0;
}

重申一下,如果我只输入1个参数,这个程序就可以了。但是,当存在多个参数时,程序会正确输出第一个参数的素数因子,但以下参数将使用第一个参数的素因子打印。其次,当你输入一个bash脚本范围(即{1..5})时,它只打印出参数而不是它们各自的素因子。如果有需要澄清的事情,请随时提出。此外,如果某个地方似乎有重复/类似的问题,我无法找到,请告诉我。感谢。

1 个答案:

答案 0 :(得分:2)

pthread_create(&t_id[i], NULL, primeFactors, *(argv+1))

您将相同的参数*(argv+1)传递给每个线程。请尝试使用*(argv+1+i)