主线程中的多线程矩阵乘法不会等到其他线程完成其工作?

时间:2015-10-30 07:27:33

标签: c multithreading

在这个程序中,主线程不会等待子线程,但是当我替换pthread_join的位置并在pthread_create之后将其置于for循环中时,主线程将等待子线程。

我认为如果我这样做,我会删除多线程程序的优点,因为主线程将在创建每个线程之后等待,直到它完成其工作,因此程序不能并行工作。

有人可以提前帮助我找到解决这个问题的方法吗?

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


int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int b[3][2]={{8,9},{7,2},{5,6}};
int c[3][2];
int a_rows=3,a_cols=3,b_rows=3,b_cols=2;


struct dimensions
{
  int row;
  int col;
};

 //method computes C[i][j] in the output C matrix**/
void *matrix_mulCell(void * arg)
{
   struct dimensions *d=arg;
   int sum=0;
   int k;
for(k=0; k<b_rows; ++k)
{
    sum+=(a[d->row][k]*b[k][d->col]);
}
c[d->row][d->col]=sum;
 /**Exit the thread*/
  pthread_exit(NULL);
}



int main()
{
     int i,j;
     pthread_t threads2[a_rows][b_cols];
     struct dimensions *d=(struct dimensions *) malloc(sizeof(struct dimensions));
    for(i=0; i<a_rows; ++i)
    {
        for(j=0; j<b_cols; ++j)
        {
            d->row=i;
            d->col=j;
            /**create thread to compute the value of element c[i][j]**/
            if( pthread_create(&threads2[i][j], NULL, matrix_mulCell, d))
            {
                printf("Can not create a thread\n");
                exit(1);
            }
        }
    }


 for(i=0; i<a_rows; ++i)
  {
    for(j=0; j<b_cols; ++j)
    {
                      /**Make sure the parent waits for all thread to complete**/
            pthread_join(threads2[i][j],NULL);
    }
}

/**print the result **/
for(i=0; i<a_rows; ++i)
{
    for(j=0; j<b_cols; ++j)
    {
        printf("%d ",c[i][j]);
    }
    printf("\n");
}
 return 0;
}

2 个答案:

答案 0 :(得分:0)

这是我添加@milevyo编辑后的答案(为每个线程创建新结构)并让主线程在程序结束时等待其他线程,然后在创建子线程之后打印矩阵

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

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int b[3][2]={{8,9},{7,2},{5,6}};
int c[3][2];

int a_rows=3,a_cols=3,b_rows=3,b_cols=2;
struct dimensions
{
  int row;
  int col;
};

//method computes C[i][j] in the output C matrix**/
void *matrix_mulCell(void * arg)
{
 struct dimensions *d=arg;
 int sum=0;
 int k;
for(k=0; k<b_rows; ++k)
{
    sum+=(a[d->row][k]*b[k][d->col]);
}
c[d->row][d->col]=sum;
/**Exit the thread*/
free(d);// thread is responsible of freeing it
pthread_exit(NULL);
}



int main(void)
{
     int i,j;
     pthread_t threads2[a_rows][b_cols];
     memset(c,0,sizeof(c));

   struct dimensions *d;
   for(i=0; i<a_rows; ++i)
   {
       for(j=0; j<b_cols; ++j)
       {
           // allocate for each thread it own struct
          // thread it self will free it
        d=malloc(sizeof(struct dimensions));
        d->row=i;
        d->col=j;
        /**create thread to compute the value of element c[i][j]**/
        if( pthread_create(&threads2[i][j], NULL, matrix_mulCell, d))
        {
            printf("Can not create a thread\n");
            exit(1);
        }
    }
}
  for(i=0; i<a_rows; ++i)
    {
        for(j=0; j<b_cols; ++j)
        {
            /**make main thread wait for child threads**/
     pthread_join(threads2[i][j],NULL);
    }
    }
/**print the result **/
for(i=0; i<a_rows; ++i)
{
    for(j=0; j<b_cols; ++j)
    {
        printf("%d ",c[i][j]);
    }
    printf("\n");
 }
 return 0;

}

答案 1 :(得分:-1)

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

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int b[3][2]={{8,9},{7,2},{5,6}};
int c[3][2];

int a_rows=3,a_cols=3,b_rows=3,b_cols=2;
struct dimensions
{
  int row;
  int col;
};

 //method computes C[i][j] in the output C matrix**/
void *matrix_mulCell(void * arg)
{
    struct dimensions *d=arg;
    int sum=0;
    int k;
    for(k=0; k<b_rows; ++k)
    {
        sum+=(a[d->row][k]*b[k][d->col]);
    }
    c[d->row][d->col]=sum;
    /**Exit the thread*/
    free(d);// thread is responsible of freeing it
    pthread_exit(NULL);
}



int main(void)
{
     int i,j;
     pthread_t threads2[a_rows][b_cols];
    memset(c,0,sizeof(c));

    struct dimensions *d;
    for(i=0; i<a_rows; ++i)
    {
        for(j=0; j<b_cols; ++j)
        {
            // allocate for each thread it own struct
            // thread it self will free it
            d=malloc(sizeof(struct dimensions));
            d->row=i;
            d->col=j;
            /**create thread to compute the value of element c[i][j]**/
            if( pthread_create(&threads2[i][j], NULL, matrix_mulCell, d))
            {
                printf("Can not create a thread\n");
                exit(1);
            }
            pthread_join(threads2[i][j],NULL);
        }
    }
    /**print the result **/
    for(i=0; i<a_rows; ++i)
    {
        for(j=0; j<b_cols; ++j)
        {
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}