567
社区成员




#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
ifstream instream;
ofstream outstream;
vector<int> v1;
int length; //全局的一个变量
//考虑到并行的粒度问题给出的填充单元
struct private_c{
int value;
char padding[60];
};
private_c private_count[5];
DWORD WINAPI count3(void* id3)
{
int id;
id=*((int*)id3);
private_count[id].value=0;
int start,end;
if(id==0)
{
start=0;
end=length/5;
}
if(id==1)
{
start=length/5;
end=(length/5)*2;
}
if(id==2)
{
start=(length/5)*2;
end=(length/5)*3;
}
if(id==3)
{
start=(length/5)*3;
end=(length/5)*4;
}
if(id==4)
{
start=(length/5)*4;
end=length;
}
for(int i=start;i<end;i++)
{
if(v1[i]==3)
{
private_count[id].value++;
}
}
}
DWORD WINAPI count3s(void* id2)
{
int id;
id=*((int*)id2);
private_count[id].value=0;
int start,end;
if(id==0)
{
start=0;
end=length/3;
}
if(id==1)
{
start=length/3;
end=(length/3)*2;
}
if(id==2)
{
start=(length/3)*2;
end=length;
}
for(int i=start;i<end;i++)
{
if(v1[i]==3)
{
private_count[id].value++;
}
}
}
//线程函数
DWORD WINAPI count3s_thread(void* id1)
{
int id;
id=*((int*)id1);
private_count[id].value=0;
int start,end;
//cout<<"线程"<<id<<"开始运行"<<endl;
if(id==0)
{
end=length/2;
start=0;
}
else
{
if(id==1)
{
end=length;
start=(length/2);
}
}
for(int i=start;i<end;i++)
{
if(v1[i]==3)
{
private_count[id].value++;
}
}
}
//串行执行的子程序
void chuan(vector<int> v)
{
int count=0;
if(v.size()==0)
{
}
else
{
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
if(*it==3)
{
count++;
}
}
}
cout<<"3的个数"<<count<<endl;
}
//主程序入口地址
int main()
{
int a;
instream.open("1.txt");
if(instream.fail())
{
cout<<"OPEN ERROR";
}
else
{
cout<<"open sucess"<<endl;
while(!instream.eof())
{
instream>>a;
v1.push_back(a);
}
cout<<"the length is:"<<v1.size()<<endl;
}
length=v1.size();
clock_t star,end;
char order;
cout<<"开始比较两个方法的时间"<<endl;
cout<<"串行执行结果如下:"<<endl;
star=clock();
chuan(v1);
end=clock();
cout<<"时间是:"<<end-star<<endl;
cout<<endl;
cout<<endl;
cout<<"2线程并行执行结果如下:"<<endl;
star=clock();
int p_count;
p_count=v1.size();
HANDLE hThread1,hThread2;
int i=0, j=1;
hThread1=CreateThread(NULL,0,count3s_thread,&i,0,NULL);
hThread2=CreateThread(NULL,0,count3s_thread,&j,0,NULL);
WaitForSingleObject(hThread1,4000);
WaitForSingleObject(hThread2,4000);
cout<<"3的个数"<<private_count[0].value+private_count[1].value<<endl;
end=clock();
cout<<"时间是:"<<end-star<<endl;
cout<<endl;
cout<<endl;
cout<<"3线程并行执行结果如下:"<<endl;
star=clock();
HANDLE Thread1,Thread2,Thread3;
int k=0, l=1,m=2;
Thread1=CreateThread(NULL,0,count3s,&k,0,NULL);
Thread2=CreateThread(NULL,0,count3s,&l,0,NULL);
Thread3=CreateThread(NULL,0,count3s,&m,0,NULL);
WaitForSingleObject(Thread1,4000);
WaitForSingleObject(Thread2,4000);
WaitForSingleObject(Thread3,4000);
cout<<"3的个数"<<((private_count[0].value+private_count[1].value)+private_count[2].value)<<endl;
end=clock();
cout<<"时间是:"<<end-star<<endl;
cout<<endl;
cout<<endl;
cout<<"5线程并行执行结果如下:"<<endl;
star=clock();
HANDLE hread1,hread2,hread3,hread4,hread5;
int n=0,o=1,p=2,q=3,r=4;
hread1=CreateThread(NULL,0,count3,&n,0,NULL);
hread2=CreateThread(NULL,0,count3,&o,0,NULL);
hread3=CreateThread(NULL,0,count3,&p,0,NULL);
hread4=CreateThread(NULL,0,count3,&q,0,NULL);
hread5=CreateThread(NULL,0,count3,&r,0,NULL);
WaitForSingleObject(hread1,10000);
WaitForSingleObject(hread2,10000);
WaitForSingleObject(hread3,10000);
WaitForSingleObject(hread4,10000);
WaitForSingleObject(hread5,10000);
cout<<"3的个数"<<(private_count[0].value+private_count[1].value+private_count[2].value+private_count[3].value+private_count[4].value)<<endl;
end=clock();
cout<<"时间是:"<<end-star<<endl;
system("pause");
}