【问题标题】:threads to perform statistical calculations on list of numbers input on commad line?线程对命令行输入的数字列表执行统计计算?
【发布时间】:2013-10-20 00:00:42
【问题描述】:

我正在尝试编写一个多线程程序,该程序从命令行获取数字列表并使用单独的工作线程计算各种统计值,例如平均值、总和等。我在这个程序中创建了三个线程并且它编译但我得到了错误。我是 C 和线程编程的新手,请指导我将数据传递给线程进行计算? 这是我的代码:

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

#define NUM_THREAD 3

int average, min, max;

void *
doSomeThing(void *param)
{

    //int *id_ptr, taskid;
    int *argv = (int *) param;
    sleep(1);
    //id_ptr=(int *) threadid;
    //taskid= *id_ptr;
    int j;
    int sum = 0;
    int upper = atoi(param);

    sleep(1);
    pthread_t id = pthread_self();

    unsigned long i = 0;


    if (id = 1) {
        int i;
        for (i = 0; i < upper; i++) {
            sum += argv[i];
        }
        printf("sum of no's is :\n", sum);
    }
    if (id = 2) {
        printf("\n Second thread processing\n");
    }
    if (id = 3) {
        printf("\n Third thread processing\n");
    }

    for (i = 0; i < -1; i++);
    {
        pthread_exit(NULL);
    }
}

int
main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREAD];
    pthread_attr_t attr;
    int *taskid[NUM_THREAD];
    int i = 0;
    int t;
    int err;
    //int input,a;
    if (argc != 2) {
        fprintf(stderr, "usage: a.out <integer value>\n");
        return -1;
    }
    /*
    printf("how many no's do u want to evaluate?:\n");
    scanf("%d", &input);
    printf("Enter the no's:\n");
    for (a = 0; a < input; a++) {
        arr[a] = (int) malloc(sizeof(int));
        scanf("%d", &arr[a]);
        printf("data:", &arr[a]);
    }
    */
    pthread_attr_init(&attr);
    for (t = 0; t < NUM_THREAD; t++) {
        taskid[t] = (int *) malloc(sizeof(int));
        *taskid[t] = t;
        printf("In main: creating thread %d\n", t);
        err = pthread_create(&threads[t], &attr, doSomeThing, argv[1]);

        if (err) {
            printf("Error; return code from pthread_create() is %d\n",
                   err);
            exit(-1);

        }
    }
    for (t = 0; t < NUM_THREAD; t++) {
        pthread_join(threads[t], NULL);
        printf("Joining thread %d\n", t);
    }
    pthread_exit(NULL);
}

【问题讨论】:

  • 那里有可怕的格式。
  • 欢迎来到 Stack Overflow。请阅读About 页面。很难阅读缩进严重的代码;请确保您的代码正确缩进。不要使用标签;用(四个 - 推荐的)空格替换它们。请详细解释您遇到的错误,并提供 SSCCE (Short, Self-Contained, Correct Example),以便人们可以更轻松地重现您看到的问题。输入数据和其他代码(例如启动线程的main())也可能是相关的。 ……哦……main()在里面,缩进了大约5层!哦!
  • 是什么让您认为 pthread_create 分配了一个不错的小数字,例如 1、2、3 作为 thread_id?当您调用pthread_self() 时,您不太可能获得1、2 或3。而且您最终应该free 获得从malloc 获得的内存。
  • 我希望教授们等到学生成为称职的程序员后再尝试教他们线程。或者至少有一个不错的硬件视角。但我想这需要数年时间,而他们没有那样的时间。
  • 我洗得很匆忙,我会尽量正确缩进代码。但是谢谢你的帮助,是的,我真的在为线程苦苦挣扎。我将初始化任务 ID,但我如何将用户输入传递给线程进行计算?

标签: c multithreading


【解决方案1】:

是什么让您认为pthread_create 分配了一个不错的小数字,例如 1、2、3 作为 thread_id?当您调用pthread_self() 时,您不太可能获得1、2 或3。而且您最终应该free 获得从malloc 获得的内存。

我的建议是您为平均值、最大值和最小值编写一个单独的函数,并显式调用 pthread_create 3 次,传入 3 个单独的函数,而不是使用一个函数来完成所有工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 2011-08-22
    • 2012-12-25
    • 1970-01-01
    • 2015-09-15
    • 2015-05-24
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多