Multithreading Posix线程程序乘法-查询代码日志如何检查它是否发出不同的线程
在下面的link博客中可以找到使用Posix线程进行矩阵乘法的示例,正如前面提到的,每个线程都保存着行和列信息,下面是link并复制了程序以供参考Multithreading Posix线程程序乘法-查询代码日志如何检查它是否发出不同的线程,multithreading,matrix,pthreads,posix,multiplication,Multithreading,Matrix,Pthreads,Posix,Multiplication,在下面的link博客中可以找到使用Posix线程进行矩阵乘法的示例,正如前面提到的,每个线程都保存着行和列信息,下面是link并复制了程序以供参考 #包括 #包括 #包括 #定义m3 #定义k2 #定义n3 #定义NUM_线程10 int A[M][K]={{1,4},{2,5},{3,6}; int B[K][N]={{8,7,6},{5,4,3}; 国际货币基金组织[M][N]; 结构v{ int i;/*行*/ int j;/*列*/ }; 无效*流道(无效*参数);/*线*/ int
#包括
#包括
#包括
#定义m3
#定义k2
#定义n3
#定义NUM_线程10
int A[M][K]={{1,4},{2,5},{3,6};
int B[K][N]={{8,7,6},{5,4,3};
国际货币基金组织[M][N];
结构v{
int i;/*行*/
int j;/*列*/
};
无效*流道(无效*参数);/*线*/
int main(int argc,char*argv[]){
int i,j,计数=0;
对于(i=0;ii=i;
数据->j=j;
/*现在,创建将数据作为参数传递给它的线程*/
pthread\u t tid;//线程ID
pthread\u attr\u t attr;//线程属性集
//获取默认属性
pthread_attr_init(&attr);
//创建线程
pthread_创建(&tid,&attr,runner,data);
//确保父线程等待所有线程完成
pthread_join(tid,NULL);
计数++;
}
}
//打印出结果矩阵
对于(i=0;ii][n]*B[n][数据->j];
}
//将总和指定给其坐标
C[数据->i][data->j]=总和;
//退出线程
pthread_退出(0);
}
询问:这真的会为每一行/列发出不同的线程吗?我们如何定义不同的线程被发布
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define M 3
#define K 2
#define N 3
#define NUM_THREADS 10
int A [M][K] = { {1,4}, {2,5}, {3,6} };
int B [K][N] = { {8,7,6}, {5,4,3} };
int C [M][N];
struct v {
int i; /* row */
int j; /* column */
};
void *runner(void *param); /* the thread */
int main(int argc, char *argv[]) {
int i,j, count = 0;
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
//Assign a row and column for each thread
struct v *data = (struct v *) malloc(sizeof(struct v));
data->i = i;
data->j = j;
/* Now create the thread passing it data as a parameter */
pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes
//Get the default attributes
pthread_attr_init(&attr);
//Create the thread
pthread_create(&tid,&attr,runner,data);
//Make sure the parent waits for all thread to complete
pthread_join(tid, NULL);
count++;
}
}
//Print out the resulting matrix
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
}
//The thread will begin control in this function
void *runner(void *param) {
struct v *data = param; // the structure that holds our data
int n, sum = 0; //the counter and sum
//Row multiplied by column
for(n = 0; n< K; n++){
sum += A[data->i][n] * B[n][data->j];
}
//assign the sum to its coordinate
C[data->i][data->j] = sum;
//Exit the thread
pthread_exit(0);
}