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

泰德

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

我试图用灰泥传递它,但我很困惑

#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

戈戈伦

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

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

将参数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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法将哈希数组传递给 Chef 中的提供程序函数

有没有办法将函数调用传递给内部对象?

有没有办法将函数从objective-c++传递给标准c++?

有没有办法将字符串传递给Average函数?

react native:有没有办法将函数传递给“onpress”?

有没有办法将全局变量传递给 $$eval 函数?

有没有办法将数组参数传递给管道?

有没有办法将3d数组的2d矩阵乘以R中的标量?

有没有办法将printf字母(%c,%d)作为函数参数传递?

有没有办法将方法名称传递给函数,并让该函数返回封装该方法的委托?

有没有办法将类类型作为参数传递给函数并在该函数内创建对象?

有没有办法将我的Java函数传递给try-catch函数?

有没有办法将相同的值传递给函数的所有参数?

有没有办法遍历传递给Elixir中的函数的所有参数?

有没有办法将多个值作为C中单个定义的宏值传递给宏函数?

当用作类型参数时,有没有办法将参数传递给类构造函数?

有没有办法将 jpa 谓词数组传递给 KOTLIN 中的标准构建器?

有没有办法将状态传递给作为道具传递的组件?

有没有办法让python脚本“知道”是否已通过管道传递给它?

有没有办法通过命令行将jvm args传递给Maven?

有没有办法让 rollapply 始终将矩阵传递给我的函数?

有没有办法将可选参数传递给函数?

有没有办法将对象的字段隐式传递给C ++中的函数

有没有办法解构传递给 onClick 函数的数据属性开玩笑?

有没有办法将 .Net Core 2 appsettings.json 属性传递给主干组件?

有没有办法通过函数将字符串值从View传递到js文件

有没有办法将依赖字段传递给 Julia 结构?

有没有办法将网格名称或参数传递给点击事件?

有没有办法将参数传递给xml?还是修改它?