gpt4 book ai didi

c - 为什么每次调用 pthread_join() 时 pthread_join() 的返回值都没有改变

转载 作者:行者123 更新时间:2023-11-30 18:51:56 28 4
gpt4 key购买 nike

如果用户在命令行中仅输入 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})时,它仅打印出参数,而不打印出它们各自的质因数。如果有什么需要澄清的地方,请随时询问。另外,如果我无法找到某处似乎存在重复/相似的问题,请告诉我。谢谢。

最佳答案

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

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

关于c - 为什么每次调用 pthread_join() 时 pthread_join() 的返回值都没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532144/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com