C - 使用多线程编程从命令行计算统计值

标签 c multithreading

我是一名初学者,正在开发一个 C 练习程序,该程序可以计算从命令行读取的数字列表的各种统计值。但是当尝试在 Linux 中编译我的代码时,我遇到了一些我不知道如何解释的错误,并且想知道是否可以从那些比我更熟悉 C 语言的人那里获得一些帮助。我已经包含了我的代码和下面的错误。

我的代码:

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

int *values;
int size;
double avg=0.0;
int minValue=0;
int maxValue=0;
void *runAvg(void *param);
void *runMin(void *param);
void *runMax(void *param);

int main (int argc, char *argv[]){
    if(argc != 2){
        fprintf(stderr, "usage: %s <integer value>\n", argv[0]);
    }
    int i;
    for(i=1; i < argc; i++){
        values=&(atoi(argv[i]));
        size++;
    }

    pthread_t avgPt[size];
    pthread_t minPt[size];
    pthread_t maxPt[size];
    pthread_attr_t attr;

    //create threads
        pthread_create(&avgPt, &attr, runAvg, values);
        pthread_create(&minPt, &attr, runMin, values);
        pthread_create(&maxPt, &attr, runMax, values);
    //wait for threads to exit
        pthread_join(avgPt, NULL);
        pthread_join(minPt, NULL);
        pthread_join(maxPt, NULL);
    //print results of threads
        printf("\n Average: %f \n",avg);
        printf("\n Minimum: %d \n",minValue);
        printf("\n Maximum: %d \n",maxValue);
}

void *runAvg(void *param){

    int sum=0;
    int i=0;
    int *values;
    values=(int*)param;
    for(i=0;i<size; i++) sum += values[i];
    avg = sum / (double)size;

    pthread_exit(0);
}

void *runMin(void *param){

    int i=0;
    int *values;
    values=(int*)param;
    minValue = values[0];

    for(i=0;i<size;i++){
        if(values[i]<minValue){
            minValue=values[i];
        }
    }
    pthread_exit(0);
}

void *runMax(void *param){

    int i=0;
    int *values;
    values=(int*)param;
    maxValue=values[0];

    for(i=0;i<size;i++){
        if(values[i] > maxValue){
            maxValue = values[i];
        }
    }
    pthread_exit(0);
}

我的编译错误:

423assign.c:20: error: lvalue required as unary ‘&’ operand
423assign.c:30: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:31: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:32: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:34: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’
423assign.c:35: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’
423assign.c:36: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’

最佳答案

你的线程函数没问题。

一个问题是没有分配一个int数组来包含atoi中的值。

此外,您的 pthread_t 变量不应该是数组。并且您的 attr 值从未初始化且未使用。

<小时/>

这是一个清理后的版本,其中已注释并修复了错误[请原谅无偿的清理方式]:

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

int *values;
int size;
double avg = 0.0;
int minValue = 0;
int maxValue = 0;
void *runAvg(void *param);
void *runMin(void *param);
void *runMax(void *param);

int
main(int argc, char **argv)
{

// NOTE/BUG: this should be "< 2" and not "!= 2"
    if (argc < 2) {
        fprintf(stderr, "usage: %s <integer value>\n", argv[0]);
        exit(1);
    }
    int i;

    --argc;
    ++argv;

// NOTE/BUG: we must allocate an int array of sufficient size
    values = calloc(argc,sizeof(int));

    for (i = 0;  i < argc;  i++) {
        values[i] = atoi(argv[i]);
        size++;
    }

// NOTE/BUG: these should _not_ be arrays
    pthread_t avgPt;
    pthread_t minPt;
    pthread_t maxPt;

// NOTE/BUG: this is unitialized and it's not set to anything, so the
// pthread_create 2nd argument can [and should be] NULL
#if 0
    pthread_attr_t attr;
#endif

    // create threads
#if 0
    pthread_create(&avgPt, &attr, runAvg, values);
    pthread_create(&minPt, &attr, runMin, values);
    pthread_create(&maxPt, &attr, runMax, values);
#else
    pthread_create(&avgPt, NULL, runAvg, values);
    pthread_create(&minPt, NULL, runMin, values);
    pthread_create(&maxPt, NULL, runMax, values);
#endif

    // wait for threads to exit
    pthread_join(avgPt, NULL);
    pthread_join(minPt, NULL);
    pthread_join(maxPt, NULL);

    // print results of threads
    printf("\n Average: %f \n", avg);
    printf("\n Minimum: %d \n", minValue);
    printf("\n Maximum: %d \n", maxValue);
}

void *
runAvg(void *param)
{

    int sum = 0;
    int i = 0;
    int *values;

    values = param;
    for (i = 0; i < size; i++)
        sum += values[i];
    avg = sum / (double) size;

    return (void *) 0;
}

void *
runMin(void *param)
{

    int i = 0;
    int *values;

    values = param;
    minValue = values[0];

    for (i = 0; i < size; i++) {
        if (values[i] < minValue)
            minValue = values[i];
    }

    return (void *) 0;
}

void *
runMax(void *param)
{

    int i = 0;
    int *values;

    values = param;
    maxValue = values[0];

    for (i = 0; i < size; i++) {
        if (values[i] > maxValue)
            maxValue = values[i];
    }

    return (void *) 0;
}

关于C - 使用多线程编程从命令行计算统计值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999503/

相关文章:

编译 C : collect2. exe: error: ld returned 1 exit status

iphone - 核心数据 - 多线程 - 启动时的竞争条件

c# - 使用 Task.WaitAll() 时如何获取任务的返回值

c++ - 在CentOS中编译C遇到错误: undefined reference to

java - jmeter:AbstractJavaSamplerClient - 对所有线程仅执行一次 setUpTest

C# 复杂线程同步

c# - 如何处理这种竞争条件?

c - undefined reference 'yylex' bison 错误

c++ - 无法弄清楚如何计算CRC7

C变量是否可以在程序运行过程中随时删除或移除?