计算数组的大小以使用线程计算平均值

标签 c multithreading size average

我有一个巨大的头文件 average.h,其中包含 4 个数组。我想通过使用线程分别计算4个数组的平均值来计算头文件的平均值。 我在运行时遇到段错误,所以我猜我计算数组长度的方式有问题。我被告知不可能计算 float** 的大小,那么我应该怎么做呢?

这是我的代码:

/*
 * File:   main.c
 * Author: thomasvanhelden
 *
 * Created on June 15, 2014, 5:34 AM
 */

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

void* calc(void* param) {
    float** array = (float**) param;
    int size = sizeof(*array) / sizeof(float*);
    int i;
    float sum = 0;
    float* average;

    for (i = 0; i < size; i++) {
        sum += *array[i];
    }

    *average = sum/size;

    return average;
}

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

    pthread_t t1, t2, t3, t4;   // thread handlers
    int res1, res2, res3, res4; // results of creating/joining the threads
    void *avg1, *avg2, *avg3, *avg4; // results of the threads as void*
    float *result1, *result2, *result3, *result4;   // results of the threads as flaot*

    // create the threads
    res1 = pthread_create(&t1, NULL, calc, a1);
    res2 = pthread_create(&t2, NULL, calc, a2);
    res3 = pthread_create(&t3, NULL, calc, a3);
    res4 = pthread_create(&t4, NULL, calc, a4);

    // check for errors creating the threads
    if (res1 || res2 || res3 || res4) {
        printf("Something went wrong creating the threads!\n");
        return 1;
    }

    // wait for the threads to finish and get the result
    res1 = pthread_join(t1, &avg1);
    res2 = pthread_join(t2, &avg2);
    res3 = pthread_join(t3, &avg3);
    res4 = pthread_join(t4, &avg4);

    // check for errors joining the threads
    if (res1 || res2 || res3 || res4) {
        printf("Something went wrong joining the threads!\n");
        return 1;
    }

    // void* to float*
    result1 = (float*) avg1;
    result2 = (float*) avg2;
    result3 = (float*) avg3;
    result4 = (float*) avg4;

    // print the result, should be
    printf("The average is: %f", (*result1 + *result2 + *result3 + *result4));


    return (EXIT_SUCCESS);
}

最佳答案

如您所说,不可能从指针检索数组的大小。您必须将您的参数打包在一个结构中(您的float**,加上您的数组大小,以及任何其他相关信息),并将指向该结构的指针传递给pthread_create().

请注意,您的辅助函数必须返回一个指针,因此需要分配内存。如果您希望避免动态分配,这里有一个模式也将参数结构重用为返回值:

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

#define ARRAY_COUNT(arr) (sizeof (arr) / sizeof *(arr))

typedef union {
    struct {    // Function parameters
        float *array;
        size_t size;
    };
    struct {    // Function return value
        float result;
    };
} arrayAverageParam_u;

void *arrayAverage(void *param) {
    arrayAverageParam_u *_param = param;
    // From now on we can use _param to access the struct

    int i;
    float avg = 0.0f;
    for(i = 0; i < _param->size; ++i)
        avg += _param->array[i];

    if(i)
        avg /= i;

    // Store the result, overwriting the parameters
    _param->result = avg;

    return NULL;
}

main()
{
    float array[] = {1.0f, 2.0f, 3.0f, 4.0f};

    // Fill the struct with parameters
    arrayAverageParam_u param = {
        .array = array,
        .size = ARRAY_COUNT(array),
    };

    pthread_t thread;
    pthread_create(&thread, NULL, arrayAverage, &param);

    pthread_join(thread, NULL);

    // Retrieve the result from the struct
    printf("The average is %g\n", param.result);

    return 0;
}

关于计算数组的大小以使用线程计算平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24262877/

相关文章:

c - 如何创建伪造的 "Loading..."序列?

C 将字符串转换为整数问题

填充数组的代码突然改变变量(C)

java - Pulsar 消息监听器的多线程

python - 多线程网络服务器与单线程

Java Instrumentation 查找对象大小

html - Bootstrap 4 使每列大小相等?

javascript - 提交时保持相同的大小

c++ - 宏字符串 : what does #define __T(x) x mean? 和 __T(#x)?

multithreading - 计算最小值的最短时间