【发布时间】:2016-10-14 11:44:58
【问题描述】:
我对 C 完全陌生,请原谅我缺乏知识。我正在尝试制作 4 个线程,每个线程分别为每个线程生成 100-199 200-299 300-399 和 400-499 之间的数字。但是,当我传递我的参数 interv 时,它是一个具有两个 int 值的结构类型,我在另一边得到了完全不同的东西。例如,当我发送 100 和 199 时,我得到 0 而不是 199 和 -13216 而不是 100。我不确定问题到底出在哪里,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
int sum; /* global variable shared by thread(s) */
pthread_mutex_t counter_lock = PTHREAD_MUTEX_INITIALIZER;
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
typedef struct interval {
int min;
int max;
} interval;
void *runner(struct interval *param); /* threads call this function */
/*
*
*/
int main(int argc, char *argv[]) {
pthread_t workers[NUM_THREADS];
interval *interv;
interv->max = 199;
interv->min = 100;
/* create the thread */
printf("min = %d max = %d \n",interv->min,interv->max);
for (int i = 0; i < NUM_THREADS; i++) {
printf("min = %d max = %d \n",interv->min,interv->max);
pthread_create(&workers[i],NULL,runner,&interv);
interv->min += 100;
interv->max += 100;
/* wait for the thread to exit */
pthread_join(&workers[i],NULL);
}
printf("sum = %d\n",sum);
return (0);
}
/* The thread will begin control in this function */
void *runner(struct interval *param) {
int n, array[100], list_sum, counter;
printf("min = %d max = %d \n",param->min,param->max);
for (int i; i < 100; i++) {
n = rand() % (param->max + 1 - param->min) + param->min;
array[i] = n;
list_sum += n;
}
qsort(array, 100, sizeof(int), cmpfunc);
for (int i; i < 100; i++) {
counter += 1;
if (counter == 10) {
counter = 0;
}
}
pthread_mutex_lock(&counter_lock);
sum += list_sum;
pthread_mutex_unlock(&counter_lock);
pthread_exit(0);
}
更新: 所以我在程序编译的时候没有得到我期望的结果,所以我重写了我的大部分代码。虽然现在,我又遇到了一些奇怪的行为,我不知道为什么。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 1
#define NUM_ELEMENTS 10
//Sum computed buy the background thread
int total = 0;
int counter = 0;
struct sum_runner_struct {
int min;
int max;
int array[NUM_ELEMENTS];
int answer;
};
//Thread function to generate a sum of 0 to N
void* runner(void* arg) {
struct sum_runner_struct *arg_struct = (struct sum_runner_struct*) arg;
int n, sum;
for (int i = 0; i<NUM_ELEMENTS; i++) {
n = rand()%(arg_struct->max + 1 - arg_struct->min) + arg_struct->min;
printf("%d ",n);
arg_struct->array[i] = n;
}
printf("\n");
for (int i = 0; i<NUM_ELEMENTS; i++) {
sum = sum + arg_struct->array[i];
printf("%d ", arg_struct->array[i]);
counter += 1;
if (counter == 10) {
printf("\n");
counter = 0;
}
}
printf("Sum: %d\n",sum);
arg_struct->answer = sum;
pthread_exit(0);
}
int main(int argc, char **argv) {
int INTERVALS[4][2] = {{100,199},{200,299},{300,399},{400,499}};
struct sum_runner_struct args[NUM_THREADS];
// Launch threads
pthread_t tids[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
args[i].min = INTERVALS[i][0];
args[i].max = INTERVALS[i][1];
//Create attributes
pthread_attr_t attr;
pthread_attr_init(&attr);
//Create Thread
pthread_create(&tids[i], &attr, runner, &args[i]);
}
//Wait until thread is done its work
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(tids[i], NULL);
printf("Sum of thread %d is %d\n", i, args[i].answer);
total += args[i].answer;
}
printf("Sum is %d\n", total);
}
在您评论我的随机数生成器之前,我知道它不是目前最好的解决方案,但这不是我的问题。我的问题是,当我在数组中为线程添加数字时,我得到一个比 6 个整数大的数字。我不确定为什么会这样。
例如,当我使用单个线程运行程序以生成 10 个元素时,我会得到如下结果:
133 143 162 129 100 108 152 156 156 119
133 143 162 129 100 108 152 156 156 119
Sum: 1364
Sum of thread 0 is 1364
Sum is 1364
RUN SUCCESSFUL (total time: 57ms)
请注意,我打印了两次数组,因此为什么有两行相同的数组。正如你所看到的(我想我把它们加起来了)如果你把数组中的数字相加,你会得到 1358,而不是 1364。我不确定是什么原因造成的。
【问题讨论】:
-
你的函数原型应该是
void *runner(void *param);。 -
注意:我想我可能已经找到了问题,但似乎找不到解决方案。当我发送参数 interv 时,我意识到我正在发送 &interv ,它是一个指向指针的指针,因此值可能不同。但是当我删除 & 时,我只是得到 RUN FAILED (exit value 1, total time: 1s)
-
你永远不会为
interv分配内存。 -
不确定这是如何导致问题的,因为在函数运行器之外一切似乎都工作正常,当我在函数外部打印最小值和最大值时,我得到了正确的值,但在内部功能编号变得有趣。
-
这是未定义的行为,因此它可能在 main 而不是函数中起作用。
标签: c multithreading pthreads parameter-passing