有没有办法通过pthread将2D数组传递给函数

时间:2019-06-14 16:01:01

标签: c pthreads

我试图理解pthreads,并且我正在这个项目中,我必须通过pthread将2d数组传递给一个函数,稍后该函数将对该数组进行一些处理

我试图通过stuct传递它,但我感到困惑

#define rows 5
#define colums 5


void *maxthread(void *size )
{   
    int (*array)[rows][colums]
    ....
    ...
}



int main ()
{   

    int array[rows][colums];
    int p,P;
    pthread_t *thread;
    int i,j,r,c;


        printf("\n give numbers to array :\n");
     for(i=0;i< grammes;i++)
        {
        for(j=0;j< stiles;j++)
        {
            printf("element [%d,%d] : ",i+1,j+1);
            scanf("%d",&array[i][j]);
        }
    }

    printf("\n matrix result :\n");
    for(i=0;i< rows;i++)
    {
        for(j=0;j< colums;j++)
        {
            printf("%d\t",array[i][j]);
        }
        printf("\n");   
    }

    printf("give number of threads\n");
    scanf("%d",&p);
    for(i=0;i<p;i++)
    P=pthread_create(&thread[i][i],NULL,maxthread,(void *));

    return 0;

我希望在数组中找到最大的数字,但首先我必须使数组通过pthread

1 个答案:

答案 0 :(得分:1)

好尝试;但是,我建议您采取一些小步骤并经常进行编译,以免将自己深深地陷入混乱的状态。一旦发现错误,请先进行修复。

鉴于您的用例(用户定义的输入),我建议使用动态内存分配。这样,用户可以指定任何大小矩阵(以及任何数量的线程)。其次,由于线程仅接受一个参数,因此将矩阵属性封装在结构中似乎是理想的。应该有一个指向数据的指针字段以及行和列的记录,所有这些都可以由用户使用(或多或少)您当前的输入代码指定。

将参数struct传递到worker函数之后,需要将其强制转换为正确的类型。请注意此处修改共享数据!如果多个线程试图同时修改它,则可以向矩阵结构添加互斥锁或信号灯。

这是概念证明:

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

typedef struct matrix {
    int rows;
    int cols;
    int **data;
} matrix;

void *maxthread(void *arg) {   
    matrix m = *((matrix *)arg);

    for (int i = 0; i < m.rows; i++) {
        for (int j = 0; j < m.cols; j++) {
            printf("[%2d]", m.data[i][j]);
        }

        puts("");
    }

    puts("");
    return NULL;
}

int main() {   
    int num_threads = 3; // or take user input
    pthread_t threads[num_threads];
    matrix m;
    m.rows = 11;         // or take user input
    m.cols = 8;
    m.data = malloc(sizeof(int *) * m.rows);

    for (int i = 0; i < m.rows; i++) {
        m.data[i] = malloc(sizeof(int) * m.cols);

        for (int j = 0; j < m.cols; j++) {
            m.data[i][j] = i * j;
        }
    }

    for (int i = 0; i < num_threads; i++) {
        pthread_t thread;
        pthread_create(&thread, NULL, maxthread, &m);
        threads[i] = thread;
    }

    for (int i = 0; i < num_threads; i++) {
        pthread_join(threads[i], NULL);
    }

    for (int i = 0; i < m.rows; i++) {
        free(m.data[i]);
    }

    free(m.data);
    return 0;
}

输出可能类似于:

[ 0][ 0][ 0][ 0][ 0][ 0][ 0][ 0]
[ 0][ 1][ 2][ 3][ 4][ 5][ 6][ 7]
[ 0][ 2][ 4][ 6][ 8][10][12][14]
[ 0][ 3][ 6][ 9][12][15][18][21]
[ 0][ 4][ 8][12][16][20][24][28]
[ 0][ 5][10][15][20][25][30][35]
[ 0][ 6][12][18][24][30][36][42]
[ 0][ 7][14][21][28][35][42][49]
[ 0][ 8][16][24][32][40][48][56]
[ 0][ 9][18][27][36][45][54][63]
[ 0][10][20][30][40][50][60][70]

[ 0][ 0][ 0][ 0][ 0][ 0][ 0][ 0]
[ 0][ 1][ 2][ 3][ 4][ 5][ 6][ 7]
[ 0][ 2][ 4][ 6][ 8][10][12][14]
[ 0][ 3][ 6][ 9][12][15][18][21]
[ 0][ 4][ 8][12][16][20][24][28]
[ 0][ 5][10][15][20][25][30][35]
[ 0][ 6][12][18][24][30][36][42]
[ 0][ 7][14][21][28][35][42][49]
[ 0][ 8][16][24][32][40][48][56]
[ 0][ 9][18][27][36][45][54][63]
[ 0][10][20][30][40][50][60][70]

[ 0][ 0][ 0][ 0][ 0][ 0][ 0][ 0]
[ 0][ 1][ 2][ 3][ 4][ 5][ 6][ 7]
[ 0][ 2][ 4][ 6][ 8][10][12][14]
[ 0][ 3][ 6][ 9][12][15][18][21]
[ 0][ 4][ 8][12][16][20][24][28]
[ 0][ 5][10][15][20][25][30][35]
[ 0][ 6][12][18][24][30][36][42]
[ 0][ 7][14][21][28][35][42][49]
[ 0][ 8][16][24][32][40][48][56]
[ 0][ 9][18][27][36][45][54][63]
[ 0][10][20][30][40][50][60][70]