2014-06-17 46 views
0

我有這個巨大的頭文件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); 
} 
+0

顯示數組的聲明。 – user694733

+0

這工作?我相信下面的語句會給你1總是在32位平臺上指針的大小總是4個字節:int size = sizeof(* array)/ sizeof(float *); –

回答

2

正如你所說的,它不可能從一個指針檢索您的數組的大小。你必須將你的參數打包在一個結構體中(你的float**,加上你的數組的大小,以及任何其他相關信息),然後把一個指向這個結構體的指針傳遞給pthread_create()

請注意,您的工作函數必須返回一個指針,因此需要分配內存。如果你想避免動態分配,這裏有一個模式可以重用參數struct作爲返回值:

#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; 
} 
+0

這是一個好主意!我正在考慮將數組的大小硬編碼到for循環中,但你的聽起來好多了。謝謝! –

+0

不要忘記接受這個答案,如果它適合問題:) – Quentin

+0

對不起,我現在做了。以爲我不得不等待幾個小時才能夠接受答案...:/ –