标题:如何用WIN API函数创建多进程并行执行程序
只看楼主
lirenjie
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-11-24
 问题点数:0 回复次数:0 
如何用WIN API函数创建多进程并行执行程序
如下,是一个用WIN API函数编写的多进程并行执行程序(实现矩阵相乘)。
#include <windows.h>
#include <iostream.h>
#include <math.h>
static int a[10][10],b[10][10],c[10][10],d[10][10],A[10][10],B[10][10];  
int s,i,j,m;
DWORD WINAPI ThreadFunc1(HANDLE Thread1)     //创建线程1 HANDKE为句柄
{
 for(i=0;i<s;i++)
 {for(j=0;j<s;j++)
 {for(m=0;m<s;m++)
   A[i][m]+=a[i][j]*b[j][m];
 }
 }
 return 0;
}
DWORD WINAPI ThreadFunc2(HANDLE Thread2)     //创建线程2
{
 for(i=0;i<s;i++)
 {for(j=0;j<s;j++)
 {for(m=0;m<s;m++)
   B[i][m]+=c[i][j]*d[j][m];
 }
 }

 return 0;
}

int main()                                  //开始写主函数
{
 HANDLE Thread1;
 HANDLE Thread2;
 
 DWORD dwThreadId1;
 DWORD dwThreadId2;

cout<<"please input the number size:"<<endl;
cin>>s;               
cout<<"please input the first square:"<<endl;     
for(i=0;i<s;i++)
{for(j=0;j<s;j++)
 cin>>a[i][j];      
};
cout<<"please input the second square:"<<endl;     
for(i=0;i<s;i++)
{for(j=0;j<s;j++)
 cin>>b[i][j];      
};
cout<<"please input the third square:"<<endl;      
for(i=0;i<s;i++)
{for(j=0;j<s;j++)
 cin>>c[i][j];      
};
cout<<"please input the forth square:"<<endl;   
 for(i=0;i<s;i++)
{for(j=0;j<s;j++)
 cin>>d[i][j];      
};
 Thread1=::CreateThread                //创建线程1
  (NULL,0,ThreadFunc1,NULL,0,&dwThreadId1);
 Thread2=::CreateThread                //创建线程2
  (NULL,0,ThreadFunc2,NULL,0,&dwThreadId2);
 
  ::WaitForSingleObject(Thread1,INFINITE);   
  ::CloseHandle(Thread1);
  ::WaitForSingleObject(Thread2,INFINITE);
  ::CloseHandle(Thread2);
  
 for(i=0;i<s;i++)
 {for(j=0;j<s;j++)
   A[i][j]+=B[i][j];
 }
 cout<<"the result is="<<endl;
 for(i=0;i<s;i++)
 {for(j=0;j<s;j++)
   cout<<A[i][j]<<" ";
   cout<<endl;
 }
 return 0;
}
但是当我自己用同样的函数编写两个多项式系数相加的程序时,编译无误,但是却出现结果不符合预期要求的情况,下面是我自己编写的程序:
#include <windows.h>
#include <iostream.h>
#include <math.h>
static int m[10],n[10],a[10],b[10];  
int i,j;
int x,y;
int s;

DWORD WINAPI ThreadFunc1(HANDLE Thread1)     //创建线程1 HANDKE为句柄
{
 for(j=0;j<s;j++)
 {for(i=0;i<s;i++)
 n[j]+=m[i];
 }
return 0;
}
DWORD WINAPI ThreadFunc2(HANDLE Thread2)     //创建线程2
{
 for(y=0;y<s;y++)
 {for(x=0;x<s;x++)
 b[y]+=a[x];
 }

 return 0;
}

int main()                                  //开始写主函数
{
 HANDLE Thread1;
 HANDLE Thread2;
 
 DWORD dwThreadId1;
 DWORD dwThreadId2;

 int x,y;
 int i,j;


cout<<"please input the value of s:"<<endl;
cin>>s;                //
cout<<"please input the first square:"<<endl;      //
for(i=0;i<s;i++)
{
 cin>>m[i];      
};

cout<<"please input the second square:"<<endl;     //
for(j=0;j<s;j++)
{
 cin>>n[j];      
};

cout<<"please input the third square:"<<endl;      //
for(x=0;x<s;x++)
{
 cin>>a[x];      
};

cout<<"please input the forth square:"<<endl;      
for(y=0;y<s;y++)
{
 cin>>b[y];      
};



 Thread1=::CreateThread                //创建线程1
  (NULL,0,ThreadFunc1,NULL,0,&dwThreadId1);
 Thread2=::CreateThread                //创建线程2
  (NULL,0,ThreadFunc2,NULL,0,&dwThreadId2);
 
  ::WaitForSingleObject(Thread1,INFINITE);   
  ::CloseHandle(Thread1);
  ::WaitForSingleObject(Thread2,INFINITE);
  ::CloseHandle(Thread2);
  
 cout<<"the first result is="<<endl;
 for(j=0;j<s;j++)
 {for (i=0;i<s;i++)
 cout<<n[j]<<" ";
   cout<<endl;
 }

 cout<<"the second result is="<<endl;
for(y=0;y<s;y++)
{for(x=0;x<s;x++)
cout<<b[y]<<" ";
   cout<<endl;
 }
 return 0;
}

我想分别将两个多项式的系数相加(两组),最后分别显示;但是得不到预期结果,请大家帮忙看看问题处在哪里,谢谢!
搜索更多相关主题的帖子: API WIN 执行程序 函数 进程 
2008-11-24 18:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-246027-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.305618 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved