435,089 Members | 2,254 Online
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

# How to multiply matrices using threads

 P: 1 In my main program i am filling 2 matrices mata and matb. Then I'm multiplying them in my thread function to get matc. However, i am getting segmentation falults Expand|Select|Wrap|Line Numbers #include #include       int mata[18][16];     int matb[16][18];     int matc[18][18];   void* compute_c_ij(void* arg) {     int k;     int n= *((int*)arg);       for(k=0;k<16;++k)     {         matc[n][n] += mata[n][k] + matb[k][n];     }           pthread_exit(NULL); }   int main() {       pthread_t thr[18];         int n,m;       int i,j;       for(i=0;i<18;++i)         for(j=0;j<16;++j)             {             mata[i][j] = (i+1) +(j+1);             }       for (i=0; i<16; ++i)         for(j=0;j<18;++j)             {             matb[i][j]= (i+1)+(2*(j+1));             }         for (i=0;i<18 ; ++i)      {      pthread_create(&thr[i],NULL,compute_c_ij,NULL);      }        for (i=0 ; i<18 ; ++i)      {       pthread_join(thr[i],NULL);       printf("%d\n",matc[i][i]);      }        pthread_exit(NULL); }           Feb 14 '12 #1