多核编程与并行计算实验报告 1.docx
《多核编程与并行计算实验报告 1.docx》由会员分享,可在线阅读,更多相关《多核编程与并行计算实验报告 1.docx(27页珍藏版)》请在冰豆网上搜索。
多核编程与并行计算实验报告1
(此文档为word格式,下载后您可任意编辑修改!
)
多核编程与并行计算实验报告
姓名:
日期:
2014年4月20日
实验一
//exa1.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
voidThreadFunc1(PVOIDparam)
{
while
(1)
{
Sleep(1000);
cout<<"ThisisThreadFunc1"<}
}
voidThreadFunc2(PVOIDparam)
{
while
(1)
{
Sleep(1000);
cout<<"ThisisThreadFunc2"<}
}
intmain()
{
inti=0;
_beginthread(ThreadFunc1,0,NULL);
_beginthread(ThreadFunc2,0,NULL);
Sleep(3000);
cout<<"end"<return0;
}
实验二
//exa2.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
usingnamespacestd;
DWORDWINAPIFunOne(LPVOIDparam){
while(true)
{
Sleep(1000);
cout<<"hello!
";
}
return0;
}
DWORDWINAPIFunTwo(LPVOIDparam){
while(true)
{
Sleep(1000);
cout<<"world!
";
}
return0;
}
intmain(intargc,char*argv[])
{
intinput=0;
HANDLEhand1=CreateThread(NULL,0,FunOne,(void*)&input,CREATE_SUSPENDED,NULL);
HANDLEhand2=CreateThread(NULL,0,FunTwo,(void*)&input,CREATE_SUSPENDED,NULL);
while(true){
cin>>input;
if(input==1)
{
ResumeThread(hand1);
ResumeThread(hand2);
}
else
{
SuspendThread(hand1);
SuspendThread(hand2);
}
};
TerminateThread(hand1,1);
TerminateThread(hand2,1);
return0;
}
实验三
//exa3.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
usingnamespacestd;
intglobalvar=false;
DWORDWINAPIThreadFunc(LPVOIDpParam)
{
cout<<"ThreadFunc"<Sleep(200);
globalvar=true;
return0;
}
intmain()
{
HANDLEhthread=CreateThread(NULL,0,ThreadFunc,NULL,0,NULL);
if(!
hthread)
{
cout<<"ThreadCreateError!
"<CloseHandle(hthread);
}
while(!
globalvar)
cout<<"Threadwhile"<cout<<"Threadexit"<return0;
}
实验四:
//exa4.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
HANDLEevRead,evFinish;
voidReadThread(LPVOIDparam)
{
WaitForSingleObject(evRead,INFINITE);
cout<<"Reading"<SetEvent(evFinish);
}
voidWriteThread(LPVOIDparam)
{
cout<<"Writing"<SetEvent(evRead);
}
intmain(intargc,char*argv[])
{
evRead=CreateEvent(NULL,FALSE,FALSE,NULL);
evFinish=CreateEvent(NULL,FALSE,FALSE,NULL);
_beginthread(ReadThread,0,NULL);
_beginthread(WriteThread,0,NULL);
WaitForSingleObject(evFinish,INFINITE);
cout<<"TheProgramisEnd"<return0;
}
实验五
//exa5.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
inttotal=100;
HANDLEevFin[2];
CRITICAL_SECTIONcs;
voidWithdrawThread1(LPVOIDparam)
{
EnterCriticalSection(&cs);
if(total-90>=0)
{
total-=90;
cout<<"Youwithdraw90"<}
else
cout<<"Youdonothavethatmuchmoney"<LeaveCriticalSection(&cs);
SetEvent(evFin[0]);
}
voidWithdrawThread2(LPVOIDparam)
{
EnterCriticalSection(&cs);
if(total-20>=0)
{
total-=20;
cout<<"Youwithdraw20"<}
else
cout<<"Youdonothavethatmuchmoney"<LeaveCriticalSection(&cs);
LeaveCriticalSection(&cs);
SetEvent(evFin[1]);
}
intmain(intargc,char*argv[])
{
evFin[0]=CreateEvent(NULL,FALSE,FALSE,NULL);
evFin[1]=CreateEvent(NULL,FALSE,FALSE,NULL);
InitializeCriticalSection(&cs);
_beginthread(WithdrawThread1,0,NULL);
_beginthread(WithdrawThread2,0,NULL);
WaitForMultipleObjects(2,evFin,TRUE,INFINITE);
DeleteCriticalSection(&cs);
cout<return0;
}
实验六:
//exa6.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#defineTHREAD_INSTANCE_NUMBER3
LONGg_fResourceInUse=FALSE;
LONGg_lCounter=0;
DWORDThreadProc(void*pData){
intThreadNumberTemp=(*(int*)pData);
HANDLEhMutex;
cout<<"ThreadProc:
"<"<if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"Mutex.Test"))==NULL){
cout<<"OpenMutexerror!
"<}
cout<<"ThreadProc"<ReleaseMutex(hMutex);
CloseHandle(hMutex);
return0;
}
intmain(intargc,char*argv[])
{
inti;
DWORDID[THREAD_INSTANCE_NUMBER];
HANDLEh[THREAD_INSTANCE_NUMBER];
HANDLEhMutex;
if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"Mutex.Test"))==NULL){
if((hMutex=CreateMutex(NULL,FALSE,"Mutex.Test"))==NULL){
cout<<"CreateMutexerror!
"<return0;
}
}
for(i=0;i{
h[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadProc,
(void*)&ID[i],
0,
&(ID[i]));
if(h[i]==NULL)
cout<<"CreateThreaderror"<else
cout<<"CreateThread:
"<}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE);
cout<<"ClosetheMutexHandle!
"<CloseHandle(hMutex);
return0;
}
实验七
//exa7.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#defineTHREAD_INSTANCE_NUMBER3
DWORDfoo(void*pData){
intThreadNumberTemp=(*(int*)pData);
HANDLEhSemaphore;
cout<<"foo:
"<"<if((hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Semaphore.Test"))==NULL){
cout<<"OpenSemaphoreerror!
"<}
cout<<"foo"<ReleaseSemaphore(hSemaphore,1,NULL);
CloseHandle(hSemaphore);
return0;
}
intmain(intargc,char*argv[])
{
inti;
DWORDThreadID[THREAD_INSTANCE_NUMBER];
HANDLEhThread[THREAD_INSTANCE_NUMBER];
HANDLEhSemaphore;
if((hSemaphore=CreateSemaphore(NULL,0,1,"Semaphore.Test"))==NULL){
cout<<"CreateSemaphoreerror!
"<return0;
}
for(i=0;i{
hThread[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)foo,
(void*)&ThreadID[i],
0,
&(ThreadID[i]));
if(hThread[i]==NULL)
cout<<"CreateThreaderror"<else
cout<<"CreateThread:
"<}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
cout<<"ClosetheSemaphoreHandle!
"<CloseHandle(hSemaphore);
return0;
}
实验八:
//exa8.cpp:
Definestheclassbehaviorsfortheapplication.
//
#include"stdafx.h"
#include"exa8.h"
#include"MainFrm.h"
#include"exa8Doc.h"
#include"exa8View.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CExa8App
BEGIN_MESSAGE_MAP(CExa8App,CWinApp)
//{{AFX_MSG_MAP(CExa8App)
ON_COMMAND(ID_APP_ABOUT,OnAppAbout)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG_MAP
//Standardfilebaseddocumentcommands
ON_COMMAND(ID_FILE_NEW,CWinApp:
:
OnFileNew)
ON_COMMAND(ID_FILE_OPEN,CWinApp:
:
OnFileOpen)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CExa8Appconstruction
CExa8App:
:
CExa8App()
{
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
/////////////////////////////////////////////////////////////////////////////
//TheoneandonlyCExa8Appobject
CExa8ApptheApp;
/////////////////////////////////////////////////////////////////////////////
//CExa8Appinitialization
BOOLCExa8App:
:
InitInstance()
{
AfxEnableControlContainer();
//Standardinitialization
//Ifyouarenotusingthesefeaturesandwishtoreducethesize
//ofyourfinalexecutable,youshouldremovefromthefollowing
//thespecificinitializationroutinesyoudonotneed.
#ifdef_AFXDLL
Enable3dControls();//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();//CallthiswhenlinkingtoMFCstatically
#endif
//Changetheregistrykeyunderwhichoursettingsarestored.
//TODO:
Youshouldmodifythisstringtobesomethingappropriate
//suchasthenameofyourcompanyororganization.
SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));
LoadStdProfileSettings();//LoadstandardINIfileoptions(includingMRU)
//Registertheapplication'sdocumenttemplates.Documenttemplates
//serveastheconnectionbetweendocuments,framewindowsandviews.
CSingleDocTemplate*pDocTemplate;
pDocTemplate=newCSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CExa8Doc),
RUNTIME_CLASS(CMainFrame),//mainSDIframewindow
RUNTIME_CLASS(CExa8View));
AddDocTemplate(pDocTemplate);
//Parsecommandlineforstandardshellcommands,DDE,fileopen
CCommandLineInfocmdInfo;
ParseCommandLine(cmdInfo);
//Dispatchcommandsspecifiedonthecommandline
if(!
ProcessShellCommand(cmdInfo))
returnFALSE;
//Theoneandonlywindowhasbeeninitialized,soshowandupdateit.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog