如何让c线程执行循环求和?

ecbunoof  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(202)

所以我有这个程序我正在工作,它的直觉是,我需要做一些操作与线程,下面的scheme:第j个线程Hj计算总和的一组100次连续迭代,在所有线程之间循环分配组。例如,如果H = 4,线程H2进行迭代计算[100..199,500..599,900..999,...]。为确保不发生数据争用,每个线程必须处理一个不同的和变量。2然后在连接线程后比较线程获得的结果和顺序完成的结果。
代码如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <sys/time.h>

#define H 4

double res[H] = {0};

//Time function

float restar_tiempo(struct timeval *inicio, struct timeval *fin) {
    return (fin->tv_sec - inicio->tv_sec) + 1e-6 * (fin->tv_usec - inicio->tv_usec);
}

//Thread function
void *_hilo(void *arg) {
    int a = * ((int*)arg);

    double pi = 0;

    double n = 100 * a;
    while (n < 10000000) {
        res[a] += (pow(-1, n) / pow(4, n)) * ((2 / (4 * n + 1)) + (2 / (4 * n + 2)) + (1 / (4 * n + 3)));
        pi++;
        n++;
        if ((int) n % 100 == 0)
            n += (H - 1)*100;
    }  
    printf("Result on thread[%d]: %f\n", a, res[a]);
    pthread_exit(NULL);
}

int main() {
    pthread_t hilo[H];
    struct timeval in, mid, fin;

    gettimeofday(&in, NULL);
    for (int i = 0; i < H; i++) {

        int* p = malloc(sizeof (int));
        *p = i;
        printf("Esto es i: %d\n", i);
        res[i] = 0;

        if (pthread_create(&hilo[i], NULL, _hilo,  p) != 0) {
            perror(" Error creando hilo");
            exit(EXIT_FAILURE);
        }

        free(p);
    }

    //Join
    for (int i = 0; i < H; i++)
        pthread_join(hilo[i], NULL);

    //Partial sum
    double f = 0;
    for (int i = 0; i < H; i++){
        printf("Resultado parcial de hilo %d: %f\n", i, res[i]);
        f += res[i];
    }
    //Total partial sum
    printf("Resultado total: %lf\n", f);
    //printf("Hola/n");
    gettimeofday(&mid, NULL);

    //Secuential sum
    double s = 0;
    for (double n = 0; n < 10000000; n++)
        s += (pow(-1, n) / pow(4, n)) * ((2 / (4 * n + 1)) + (2 / (4 * n + 2)) + (1 / (4 * n + 3)));

    //Print secuential
    printf("Resultado secuencial: %f\n", s);
    gettimeofday(&fin, NULL);

    //Result diff
    printf("Diferencia resultados: %f\n", fabs(f - s));

    //Time threads
    printf("Tiempo por hilos: %f\n", restar_tiempo(&in, &mid));

    //Secuential time
    printf("Tiempo secuencial: %f\n", restar_tiempo(&mid, &fin));

    //Time diff
    printf("Diferencia tiempos: %f\n", restar_tiempo(&in, &mid) - restar_tiempo(&mid, &fin));

    return 0;
}

我可以编译任何东西而不发出警告,但是当我执行程序时,第一个线程提供的结果是不稳定的,因为它在执行之间会发生变化(其余的线程显示0,因为它们处理的值很少)。

First execution:

This is i:0
This is i:1
This is i:2
This is i:3
//Inside thread funct
Thread result[2]: 0.000000
Thread result[2]: 0.000000
Thread result[3]: 0.000000
Thread result[0]: 3.141593
//After join
Partial result of thread 0: 3.141593
Partial result of thread 1: 0.000000
Partial result of thread 2: 0.000000
Partial result of thread 3: 0.000000
Total result: 3.141593
Sequential result: 3.141593
Difference results: 0.000000
Time per threads: 0.183857
Sequential time: 0.034788
Difference times: 0.149069

Second execution:

This is i:0
This is i:1
This is i:2
This is i:3
Thread result[2]: 0.000000
Thread result[0]: 6.470162
Thread result[0]: 6.470162
Thread result[3]: 0.000000
Partial result of thread 0: 6.470162
Partial result of thread 1: 0.000000
Partial result of thread 2: 0.000000
Partial result of thread 3: 0.000000
Total result: 6.470162
Sequential result: 3.141593
Difference results: 3.328570
Time per threads: 0.189794
Sequential time: 0.374017
Difference times: -0.184223

我怎样才能使它正确地工作呢?我认为这与函数_hilo中的arg有关,或者与随后的int转换有关。(请原谅语言的混合,我说西班牙语,所以大多数printfs都是用说的语言。不要介意他们,结果示例的块有翻译)

neekobn8

neekobn81#

好吧,我解决了它,但我不知道为什么它这样工作完全或为什么这引起的问题。我只是删除了free (p)语句,现在它的工作就像一个魅力。如果有人能启发我为什么会发生这种情况,我会很感激。

相关问题