test.cpp

来自「linux多线程矩阵相乘(C++) 采用多线程技术」· C++ 代码 · 共 56 行

CPP
56
字号
// project created on 09-3-29 at 下12:03#include <stdio.h>#include <pthread.h>const int row=4;const int column1=5;const int column2=4;int sz1[row][column1]={1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8};
int sz2[column1][column2]={1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7,5,6,7,8};
int szRes[row][column2]={0};
 
const int threadNumber=row*column2;struct IANDJ{int i;int j;};

void * funCal(void * arg)
{    IANDJ *temp=(IANDJ *)arg;    for(int i=0;i<column1;i++)
      szRes[temp->i][temp->j]+=sz1[temp->i][i]*sz2[i][temp->j];
}	int main (int argc, char *argv[]){	IANDJ *iandjs[threadNumber];        for(int i=0;i<threadNumber;i++)           iandjs[i]=new IANDJ();	pthread_t hThread[threadNumber];    for (int i = 0; i < row; i++)     {          for(int j=0;j< column2;j++)             {	       iandjs[i*row+j]->i=i;             iandjs[i*row+j]->j=j;             pthread_create(&hThread[i*row+j], NULL, funCal,iandjs[i*4+j]);              }      }    for (int i = 0; i < threadNumber; i++)       pthread_join(hThread[i], NULL);    for(int i=0;i<row;i++)      {	     for(int j=0;j<column2;j++)	       printf("%d ",szRes[i][j]);	        printf("\n");       }	 return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?