資源簡介
機械工業出版社 并行程序設計導論 第五章練習題5.6自己寫來練手的。
代碼片段和文件信息
#include?“stdafx.h“
#include?
#include?
#include??
#include?
#include
#include
#include
#include??
#include?
#include???
using?namespace?std;
#define?Nr??5
int?main(){
queue?d;
//string?*d=new?string[Nr];
/*for(int?i=0;i {
d[i]=““;
}*/
//信號燈
int?empty=5;//隊列中的空閑空間大小
int?full=0;//隊列中已使用空間的大小
????int?nthreads;
int?tiditip;
int?first;
omp_lock_t?lockP;
//當前生產者的數目
int?numberOfProductor=0;
//初始化鎖
omp_init_lock(&lockP);
//線程總數目為4
omp_set_num_threads(4);
first=1;
//omp_in_parallel();
//omp_set_dynamic(20);
#pragma?omp?parallel??shared(demptyfullnumberOfProductor)?private(tiditip)?firstprivate(first)?
{
if(first==1)
{
tid=omp_get_thread_num();
first=0;
nthreads?=?omp_get_num_threads();
printf(“Number?of?threads?=?%d\n“?nthreads);
printf(“Thread?%d?starting...\n“?tid);
tip=?tid?%?2;
}
//偶數線程為生產者
if(tip==0)
{???//?producator
??//omp_set_lock(&lockP);
stringstream?strStream;??
strStream?< string?fileName;
string?directPath;
?char???buffer[MAX_PATH];???
????????????????????getcwd(buffer?MAX_PATH);???
??directPath=buffer;
??fileName=directPath+“\\“+strStream.str()+“.txt“;
?//fileName=strStream.str()+“.txt“;
ifstream?fin(fileName);
while(!fin.eof())
{
if(empty>0)
{
????????#pragma?omp?critical
???????{
????printf(“線程?%d?生產者?\n“tid);
printf(“生產者產生的數據為:“);
???string?str;
???getline(finstr)?;
???d.push(str);
???cout< ???????empty--;
???full++;
????????}
}
else
{
Sleep(1000);
}
}
printf(“線程?%d退出!\n“tid);
#pragma?omp?critical
{
numberOfProductor++;
?}
?//omp_unset_lock(&lockP);??
}
?else//奇數線程為消費者
{?
??//omp_set_lock(&lockP);
????//?consumator
???while(true)
???{
???if(numberOfProductor==2&&d.empty())
???{
???break;
??}
???if(full>0&&!d.empty())
???{
#pragma?omp?critical
{
printf(“線程?%d?消費者\n“tid);
char?sentences[255];
string?str;
str=d.front();
//d.pop();
str.copy(sentencesstr.length()0);?
sentences[str.length()?+?1]?=?‘\0‘;
char?*tokenPtr=strtok(sentences“?“);
printf(“消費者分詞后的數據為:\n“);
while(tokenPtr!=NULL)
{
?cout< ?tokenPtr=strtok(NULL“?“);
}
full--;
empty++;
}
???}
???else
???{
???Sleep(100);
???}
???}
??
?}
?//omp_unset_lock(&lockP);??
//}
}
??
char?end=getch();
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\
?????文件?????????246??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\1.txt
?????文件?????????143??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\2.txt
?????文件?????????172??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\3.txt
?????文件?????????148??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\4.txt
?????文件??????122368??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\OpenMPP-C.exe
?????文件?????1069528??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\OpenMPP-C.ilk
?????文件?????1559552??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\Debug\OpenMPP-C.pdb
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\ipch\
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\ipch\openmpp-c-cd764e05\
?????文件?????2359296??2014-06-10?10:32??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\ipch\openmpp-c-cd764e05\openmpp-c-eb288db.ipch
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\
?????文件????33116160??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C.sdf
?????文件?????????894??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C.sln
?????文件???????18944??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C.suo
?????文件?????????216??2014-06-10?10:18??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\0.txt
?????文件?????????131??2014-06-10?10:17??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\2.txt
?????目錄???????????0??2014-06-10?10:36??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\
?????文件?????????246??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\1.txt
?????文件?????????143??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\2.txt
?????文件?????????172??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\3.txt
?????文件?????????148??2014-06-09?20:41??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\4.txt
?????文件????????3050??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\cl.command.1.tlog
?????文件???????37912??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\CL.read.1.tlog
?????文件????????1684??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\CL.write.1.tlog
?????文件???????????2??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\li
?????文件???????????2??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\li
?????文件???????????2??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\li
?????文件???????????2??2014-06-10?10:33??并行openmp程序生產者消費者-渠澗濤-104753130775\OpenMPP-C\OpenMPP-C\Debug\li
............此處省略33個文件信息
評論
共有 條評論