简体   繁体   中英

Global variables not correctly acquired in C thread programming

I'm kind of a newbie in thread programming, I'm developing a small exercise, here is the exercise text:

Write a C program using Pthreads that implements the product of two matrices. The main thread creates nr1*nc2 threads, every thread performs its computation. Finally the main thread print the product matrix.

This is my program, wrote using C language

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

#define N 3

typedef struct {
    int rm1;
    int cm2;
} tipe_rc;

int matr1[N][N], matr2[N][N], result[N][N];

void * multiply_matrix(void *arg);

int main(int argc, const char * argv[]) {
    int matr1[N][N], matr2[N][N], i, j, k=0;
    pthread_t matr_pthread[N][N];
    void* retval;
    tipe_rc *trc;

    // fill the two matrix
    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) {
            matr1[i][j] = k++;
        }
    }

    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) {
            matr2[i][j] = k++;
        }
    }

    // start the thread computation
    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) {
            trc = malloc(sizeof(tipe_rc));
            trc->rm1 = i;
            trc->cm2 = j;
            pthread_create(&matr_pthread[i][j], NULL, multiply_matrix, (void*) trc);
        }
    }

    // rejoin all the threads
    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) {
            pthread_join(matr_pthread[i][j], &retval);
        }
    }

    // print result matrix
    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}


void * multiply_matrix(void *arg) {
    int i=0;
    tipe_rc *trc = (tipe_rc*) arg;
    result[trc->rm1][trc->cm2] = 0;
    for (i=0; i<N; i++) {
        result[trc->rm1][trc->cm2] += matr1[trc->rm1][i] * matr2[i][trc->cm2];
    }
    return NULL;
}

The code seems to be easy and pretty straight-forward, but somehow, I manage to encounter some problems with the two matrix.

THE PROBLEM

Basically, when I try to utilize the matrix matr1 and matr2 in a thread, it seems that the matrix are empty (all the values to zero), meanwhile as soon as the two matrix are generated in the main() they got correct values.

Why is that? Am I doing something wrong according to shared memory space between a process and the generated threads?

You have two each of the matr1 and matr2 variables. One set at the global level, and one set being local inside the main function, which then shadows the global variables.

Since the threads uses the global variables, they will be all zero (as global variables are zero initialized).

The simple solution is to not redeclare the variables inside the main function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM