简体   繁体   English

在C线程编程中未正确获取全局变量

[英]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. 使用Pthreads编写一个实现两个矩阵的乘积的C程序。 The main thread creates nr1*nc2 threads, every thread performs its computation. 主线程创建nr1 * nc2线程,每个线程执行其计算。 Finally the main thread print the product matrix. 最后,主线程打印乘积矩阵。

This is my program, wrote using C language 这是我的程序,用C语言写的

#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. 基本上,当我尝试在线程中使用矩阵matr1matr2时,矩阵似乎是空的(所有值都为零),与此同时,一旦在main()中生成两个矩阵,它们就会获得正确的值。

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. 每个都有两个 matr1matr2变量。 One set at the global level, and one set being local inside the main function, which then shadows the global variables. 一组在全局级别,而另一组在main函数内部,则这会遮盖全局变量。

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. 一种简单的解决方案是不重新声明main函数中的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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