通过在 pthread_create() 函数中发送结构来进行多线程矩阵加法?

发布于 2024-10-19 18:23:10 字数 1879 浏览 2 评论 0原文

我在下面的代码中的目的是通过单个进程和多线程程序添加二维矩阵元素。由于 pthread_create() 函数只需要 4 个参数,所以我想通过封装在结构中来发送两个参数。但我无法发送这是正确的。请帮助我,我可以在这段代码中做什么?

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

void *addition (void *ptr);

struct numbers {
   int num1,num2;
};

pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

main() {
  int matrix_1[10][10];
  int matrix_2[10][10];
  int matrix_3[10][10];
  int iret[10][10];

  int i,j;

  struct numbers num[10][10];
  struct numbers *ptr;

  ptr= (struct numbers *) malloc (sizeof(struct numbers));
  ptr=num;

   srand(time(NULL));

   for(i=0;i<10;i++) 
   {
      for(j=0;j<10;j++)
       {
            matrix_1[i][j]=rand()%100;
       }
   }

   for(i=0;i<10;i++)
   {
      for(j=0;j<10;j++)
       {
            matrix_2[i][j]=rand()%100;
       }
   }


   for(i=0;i<10;i++)
   {
      for(j=0;j<10;j++)
       {
            matrix_3[i][j]=matrix_1[i][j]+matrix_2[i][j];
       }
   }

pthread_t thread[100];

for(i=0;i<10;i++)
{
   for(j=0;j<10;j++)
   {

        iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);

    if ( iret[i][j]!=0 )          {
     printf("error creating thread.");
     abort();
    }
    pthread_join( thread[i][j], NULL);

    if ( pthread_join ( thread[i][j], NULL ) )     {
    printf("error joining thread.");
    abort();
    }
  }

   exit(0);
}

void *addition (void *ptr)
{
int i,j;
int matrix_toplam[10][10];
struct numbers *my_ptr;

my_ptr = (struct numbers *)malloc (sizeof(struct numbers));
my_ptr=(struct numbers *)ptr;

 for(i=0;i<10;i++)
 {
    for(j=0;j<10;j++)
    {  
       pthread_mutex_lock(&mymutex);
       matrix_toplam[i][j]=my_ptr[i][j]->num1 + my_ptr[i][j]->num2;

       pthread_mutex_unlock(&mymutex);  
    }
 }
}

my purpose in the following code is adding 2 dimensional matrix elements by a single process and by a multi threaded program.Since pthread_create() function takes only 4 parameter,I want to send two parameters by encapsulating in a structure.But I can not send it rightly.Please help me,what can I do in this code?

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

void *addition (void *ptr);

struct numbers {
   int num1,num2;
};

pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

main() {
  int matrix_1[10][10];
  int matrix_2[10][10];
  int matrix_3[10][10];
  int iret[10][10];

  int i,j;

  struct numbers num[10][10];
  struct numbers *ptr;

  ptr= (struct numbers *) malloc (sizeof(struct numbers));
  ptr=num;

   srand(time(NULL));

   for(i=0;i<10;i++) 
   {
      for(j=0;j<10;j++)
       {
            matrix_1[i][j]=rand()%100;
       }
   }

   for(i=0;i<10;i++)
   {
      for(j=0;j<10;j++)
       {
            matrix_2[i][j]=rand()%100;
       }
   }


   for(i=0;i<10;i++)
   {
      for(j=0;j<10;j++)
       {
            matrix_3[i][j]=matrix_1[i][j]+matrix_2[i][j];
       }
   }

pthread_t thread[100];

for(i=0;i<10;i++)
{
   for(j=0;j<10;j++)
   {

        iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);

    if ( iret[i][j]!=0 )          {
     printf("error creating thread.");
     abort();
    }
    pthread_join( thread[i][j], NULL);

    if ( pthread_join ( thread[i][j], NULL ) )     {
    printf("error joining thread.");
    abort();
    }
  }

   exit(0);
}

void *addition (void *ptr)
{
int i,j;
int matrix_toplam[10][10];
struct numbers *my_ptr;

my_ptr = (struct numbers *)malloc (sizeof(struct numbers));
my_ptr=(struct numbers *)ptr;

 for(i=0;i<10;i++)
 {
    for(j=0;j<10;j++)
    {  
       pthread_mutex_lock(&mymutex);
       matrix_toplam[i][j]=my_ptr[i][j]->num1 + my_ptr[i][j]->num2;

       pthread_mutex_unlock(&mymutex);  
    }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-10-26 18:23:10

看看你的行

iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);

第四个参数应该是 void* 根据手册,而不是(void)。您还需要该项目的地址,可以通过将 & 添加到 ptr[i][j] 轻松完成

您的代码需要清理,因此有我的您需要的简化版本:

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

void *addition (void *ptr);

struct numbers {
   int num1, num2, res;
};

int main() {
  pthread_t thread[10][10];
  struct numbers num[10][10];

  srand(time(NULL));


  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
          num[i][j].num1 = rand() % 100;
          num[i][j].num2 = rand() % 100;

          int ret = pthread_create( &thread[i][j], NULL, addition, 
                        (void *) &num[i][j]);

          if ( ret != 0 )          {
           printf("error creating thread.");
           abort();
          }
     }
  }

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {

      if ( pthread_join ( thread[i][j], NULL ) )     {
        printf("error joining thread.");
        abort();
          }
     }
  }

  printf("Results:\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
         printf("%3d ", num[i][j].num1);
     }
     printf("\n");
  }

  printf("\nplus\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
         {
         printf("%3d ", num[i][j].num2);
     }
     printf("\n");
  }

  printf("\nis equal to\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
         printf("%3d ", num[i][j].res);
     }
     printf("\n");
  }

  exit(0);
}

void *addition (void *ptr) {
    struct numbers *my_ptr = (struct numbers *) ptr;
    my_ptr->res = my_ptr->num1 + my_ptr->num2;

    return NULL;
}

//gcc -Wall -std=c99 -lpthread prog.c

Take a look on your line

iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);

The 4th parameter should be void* according the manual, not (void). You also need a adress of the item which could be easily done by adding & to ptr[i][j]

Your code would need to clean, thefore there is my simplified version of what you need:

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

void *addition (void *ptr);

struct numbers {
   int num1, num2, res;
};

int main() {
  pthread_t thread[10][10];
  struct numbers num[10][10];

  srand(time(NULL));


  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
          num[i][j].num1 = rand() % 100;
          num[i][j].num2 = rand() % 100;

          int ret = pthread_create( &thread[i][j], NULL, addition, 
                        (void *) &num[i][j]);

          if ( ret != 0 )          {
           printf("error creating thread.");
           abort();
          }
     }
  }

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {

      if ( pthread_join ( thread[i][j], NULL ) )     {
        printf("error joining thread.");
        abort();
          }
     }
  }

  printf("Results:\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
         printf("%3d ", num[i][j].num1);
     }
     printf("\n");
  }

  printf("\nplus\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
         {
         printf("%3d ", num[i][j].num2);
     }
     printf("\n");
  }

  printf("\nis equal to\n");

  for(int i=0;i<10;i++)
  {
     for(int j=0;j<10;j++)
     {
         printf("%3d ", num[i][j].res);
     }
     printf("\n");
  }

  exit(0);
}

void *addition (void *ptr) {
    struct numbers *my_ptr = (struct numbers *) ptr;
    my_ptr->res = my_ptr->num1 + my_ptr->num2;

    return NULL;
}

//gcc -Wall -std=c99 -lpthread prog.c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文