为什么 pthread_join() 的 return 值不会随着每次调用 pthread_join() 而改变

Why is the return value of pthread_join() not changing with each call to 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() 的 return 值的任何信息,并搜索了 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})时,它只会打印出参数而不是它们各自的质因数。如果有什么需要澄清的,请随时提出。另外,如果似乎有 duplicate/similar 个我找不到的问题,请告诉我。谢谢。

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

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