请考虑以下代码:
cuckoo.c
#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);
}
答案 0 :(得分:2)
查看第一个程序的compilation result
看起来int input
在传递到您的线程时使用相同的内存位置,而不是像您期望的那样使用新的内存位置,从而导致race condition。
解决问题的一种方法是使用array
input
...
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
分配的内存。