有没有办法将二维数组传递给 pthreads 函数?

Is there a way to pass a 2D array into a pthreads function?

我正在尝试了解 pthreads,我正在从事这个项目,我必须通过 pthreads 将二维数组传递给一个函数,该函数稍后会对数组进行一些处理。

我试过用 struct 传递它,但我很困惑。

#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 传递数组。

很好的尝试;但是,我建议采取小步骤并经常编译,以避免让自己陷入太深的混乱状态。一旦发现错误,请在继续之前修复它。

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

将参数结构传递给辅助函数后,您需要将其转换为正确的类型。在这里修改共享数据要小心!如果多个线程要同时尝试修改它,您可以向矩阵结构添加互斥锁或信号量。

这是一个概念证明:

#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]