Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:57:46+00:00 2026-05-20T09:57:46+00:00

my purpose in the following code is adding 2 dimensional matrix elements by a

  • 0

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);  
    }
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:57:47+00:00Added an answer on May 20, 2026 at 9:57 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the main purpose of the following JavaScript code? <script> var QunarUtil=new function(){var
Does the following code render the using(...) function/purpose irrelevant? Would it cause a deficiency
One recommends me the following code apparently only in .zshrc without explaining its purpose
I run the following code on a Windows platform. The purpose is the know
I am not sure about the exact purpose of the following Rampion's code .
I've got following (simplified for example purpose) code and it works: void log(const string
Purpose: To convert the following code from Selenium IDE to RC in C#. <tr>
I took the following code example from a Struts2 textbook, the purpose of the
Can anyone tell me the purpose of the following code and when/where you would
I have the following code: its purpose is very clear => at login update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.