文章引用了部分代码;http://www.cnblogs.com/zhangchaoyang/articles/1853822.html
以下是我的代码
1、多线程的代码
#include<stdio.h>
#include<time.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<memory.h>
/*定义矩阵中元素的上限,避免相乘后溢出*/
#define RANGE 100
/*矩阵A有M行N列,矩阵B有N行M列*/
#define M 1000
#define N 1000
#define P 500
int pthred_count=8;
void gene_matrix();
void read_matrix();
void *a11_b11();//1
void *a12_b21();//2
void *a11_b12();//3
void *a12_b22();//4
void *a21_b11();//5
void *a22_b21();//6
void *a21_b12();//7
void *a22_b22();//8
void output_result();
int matrixA[M][N];
int matrixB[N][M];
int matrixResult1[M][N]={0};
int matrixResult2[M][N]={0};
//int arr[pthred_count][M][N];
void *func(void *arg);
main()
{
gene_matrix(); //用随机数产生两个待相乘的矩阵,并分别存入两个文件中
read_matrix(); //从两个文件中读出数据赋给matrixA和matrixB
clock_t start=clock(); //开始计时
int res[M][N]={0}; //保存矩阵相乘的结果。非全局变量一定要显示初始化为0,否则为随机的一个数
int i=0,j,k;
pthread_t tids[8];
pthread_create(&tids[0],NULL,a11_b11,NULL);
pthread_create(&tids[1],NULL,a12_b21,NULL);
pthread_create(&tids[2],NULL,a11_b12,NULL);
pthread_create(&tids[3],NULL,a12_b22,NULL);
pthread_create(&tids[4],NULL,a21_b11,NULL);
pthread_create(&tids[5],NULL,a22_b21,NULL);
pthread_create(&tids[6],NULL,a21_b12,NULL);
pthread_create(&tids[7],NULL,a22_b