gpt4 book ai didi

c - 不一致的结果和 pthread 混淆

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:24 25 4
gpt4 key购买 nike

请考虑以下代码:

布谷鸟

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

void *thread_fn(void *vargp);

int main(int argc, char **argv){
if (argc < 2){
fprintf(stderr, "Meh, error!\n");
return 1;
}
pthread_t span[argc];
for (int i=1; i < argc; i++){
int input = atoi(argv[i]);
printf("input: argv[%d] = %s\n",i, argv[i]);
int rc = pthread_create(&span[i], NULL, thread_fn, &input);
assert (rc == 0);
}
for (int i=1; i < argc; i++){
int *output;
int err = pthread_join(span[i], (void **)&output);
assert (err == 0);
printf("in main: from thread %lu, input = %s, output = %d\n", span[i], argv[i], *output);
free(output);
}
}

void *thread_fn(void *vargp){
int *input = (int *)vargp;
int *output = malloc( sizeof(*output) );
for (int i=0; i <= *input; i++){
*output += i;
}
printf("in thread_fn: %lu, input = %d, output = %d\n", pthread_self(), *input, *output);
pthread_exit(output);
}

当我使用单独的参数运行它时,它表现良好:

$ ./a.out 4
input: argv[1] = 4
in thread_fn: 139691607996160, input = 4, output = 10
in main: from thread 139691607996160, input = 4, output = 10
$ ./a.out 5
input: argv[1] = 5
in thread_fn: 140564160374528, input = 5, output = 15
in main: from thread 140564160374528, input = 5, output = 15

但是,如果传递了多个参数,它就会失败:

$ ./a.out $(seq 1 5)
input: argv[1] = 1
input: argv[2] = 2
in thread_fn: 139922608498432, input = 2, output = 3
input: argv[3] = 3
input: argv[4] = 4
in thread_fn: 139922518308608, input = 4, output = 10
input: argv[5] = 5
in thread_fn: 139922375698176, input = 5, output = 15
in thread_fn: 139922600105728, input = 5, output = 15
in thread_fn: 139922509915904, input = 5, output = 15
in main: from thread 139922608498432, input = 1, output = 3
in main: from thread 139922600105728, input = 2, output = 15
in main: from thread 139922518308608, input = 3, output = 10
in main: from thread 139922375698176, input = 4, output = 15
in main: from thread 139922509915904, input = 5, output = 15

我在这里做错了什么?不推荐这种方法吗?我能够使用如下所示的结构来完成此操作,但我仍然无法使上面的代码段具有类似的功能。我仍然想学习和修复上面粘贴的代码。

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

void *pappanava(void *vargp);
struct payload{
int input;
int sum;
};


int main(int argc, char **argv){
if (argc < 2){
fprintf(stderr, "Invalid usage\n");
return 1;
}

//struct payload *this = malloc( sizeof(*this) );
pthread_t span[argc];
for (int i=1; i< argc; i++){
printf("i/p: %s\n", argv[i]);
struct payload *this = malloc( sizeof(*this) );
this->input = atoi(argv[i]);
int rc = pthread_create(&span[i], NULL, pappanava, &this->input);
}

for (int i=1; i< argc; i++){
struct payload *this;
pthread_join(span[i], (void**)&this);
printf("In _main_: thread: %lu, input: %d, sum: %d\n", span[i], this->input, this->sum);
free(this);
}

return 0;

}


void *pappanava(void *vargp){
struct payload *this = ( struct payload *) vargp;
int sum = 0;
for (int i=0; i <= this->input; i++){
sum += i;
}

this->sum = sum;
printf("In fn: thread: %lu, input: %d, sum: %d\n", pthread_self(), this->input, this->sum);

pthread_exit(this);
}

最佳答案

查看 compilation result第一个程序的

看起来 int input 在传递给您的线程时使用相同的内存位置,而不是像您期望的那样使用新的内存位置,从而导致 race condition .

解决问题的一种方法是使用 inputarray

...
pthread_t span[argc];
int input[argc];
for (int i=1; i< argc; i++){
input[i] = atoi(argv[i]);
...
int rc = pthread_create(&span[i], NULL, thread_fn, &input[i]);
...

其他方法是为输入分配内存

 ...
pthread_t span[argc];
for (int i=1; i< argc; i++){
int *input=malloc(sizeof(int));
*input = atoi(argv[i]);
...
int rc = pthread_create(&span[i], NULL, thread_fn, input);
...

当然,使用此解决方案来避免内存泄漏,您可以将其留给线程过程来释放为 input 分配的内存。

关于c - 不一致的结果和 pthread 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459057/

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