通过在 pthread_create() 函数中发送结构来进行多线程矩阵加法?
我在下面的代码中的目的是通过单个进程和多线程程序添加二维矩阵元素。由于 pthread_create() 函数只需要 4 个参数,所以我想通过封装在结构中来发送两个参数。但我无法发送这是正确的。请帮助我,我可以在这段代码中做什么?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
void *addition (void *ptr);
struct numbers {
int num1,num2;
};
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
main() {
int matrix_1[10][10];
int matrix_2[10][10];
int matrix_3[10][10];
int iret[10][10];
int i,j;
struct numbers num[10][10];
struct numbers *ptr;
ptr= (struct numbers *) malloc (sizeof(struct numbers));
ptr=num;
srand(time(NULL));
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_1[i][j]=rand()%100;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_2[i][j]=rand()%100;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_3[i][j]=matrix_1[i][j]+matrix_2[i][j];
}
}
pthread_t thread[100];
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);
if ( iret[i][j]!=0 ) {
printf("error creating thread.");
abort();
}
pthread_join( thread[i][j], NULL);
if ( pthread_join ( thread[i][j], NULL ) ) {
printf("error joining thread.");
abort();
}
}
exit(0);
}
void *addition (void *ptr)
{
int i,j;
int matrix_toplam[10][10];
struct numbers *my_ptr;
my_ptr = (struct numbers *)malloc (sizeof(struct numbers));
my_ptr=(struct numbers *)ptr;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
pthread_mutex_lock(&mymutex);
matrix_toplam[i][j]=my_ptr[i][j]->num1 + my_ptr[i][j]->num2;
pthread_mutex_unlock(&mymutex);
}
}
}
my purpose in the following code is adding 2 dimensional matrix elements by a single process and by a multi threaded program.Since pthread_create() function takes only 4 parameter,I want to send two parameters by encapsulating in a structure.But I can not send it rightly.Please help me,what can I do in this code?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
void *addition (void *ptr);
struct numbers {
int num1,num2;
};
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
main() {
int matrix_1[10][10];
int matrix_2[10][10];
int matrix_3[10][10];
int iret[10][10];
int i,j;
struct numbers num[10][10];
struct numbers *ptr;
ptr= (struct numbers *) malloc (sizeof(struct numbers));
ptr=num;
srand(time(NULL));
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_1[i][j]=rand()%100;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_2[i][j]=rand()%100;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
matrix_3[i][j]=matrix_1[i][j]+matrix_2[i][j];
}
}
pthread_t thread[100];
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
iret[i][j] = pthread_create( &thread[i][j], NULL ,addition,(void)ptr[i][j]);
if ( iret[i][j]!=0 ) {
printf("error creating thread.");
abort();
}
pthread_join( thread[i][j], NULL);
if ( pthread_join ( thread[i][j], NULL ) ) {
printf("error joining thread.");
abort();
}
}
exit(0);
}
void *addition (void *ptr)
{
int i,j;
int matrix_toplam[10][10];
struct numbers *my_ptr;
my_ptr = (struct numbers *)malloc (sizeof(struct numbers));
my_ptr=(struct numbers *)ptr;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
pthread_mutex_lock(&mymutex);
matrix_toplam[i][j]=my_ptr[i][j]->num1 + my_ptr[i][j]->num2;
pthread_mutex_unlock(&mymutex);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看你的行
第四个参数应该是
void*
根据手册,而不是(void)
。您还需要该项目的地址,可以通过将&
添加到ptr[i][j]
轻松完成您的代码需要清理,因此有我的您需要的简化版本:
Take a look on your line
The 4th parameter should be
void*
according the manual, not(void)
. You also need a adress of the item which could be easily done by adding&
toptr[i][j]
Your code would need to clean, thefore there is my simplified version of what you need: