多核编程与并行计算实验报告 1.docx

上传人:b****0 文档编号:12472912 上传时间:2023-04-19 格式:DOCX 页数:17 大小:402.23KB
下载 相关 举报
多核编程与并行计算实验报告 1.docx_第1页
第1页 / 共17页
多核编程与并行计算实验报告 1.docx_第2页
第2页 / 共17页
多核编程与并行计算实验报告 1.docx_第3页
第3页 / 共17页
多核编程与并行计算实验报告 1.docx_第4页
第4页 / 共17页
多核编程与并行计算实验报告 1.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

多核编程与并行计算实验报告 1.docx

《多核编程与并行计算实验报告 1.docx》由会员分享,可在线阅读,更多相关《多核编程与并行计算实验报告 1.docx(17页珍藏版)》请在冰豆网上搜索。

多核编程与并行计算实验报告 1.docx

多核编程与并行计算实验报告1

(此文档为word格式,下载后您可任意编辑修改!

 

多核编程与并行计算实验报告

 

姓名:

 

日期:

2014年4月20日

实验一

exa1.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.()

{

inti=0;

_beginthread(ThreadFunc1,0,NULL);

_beginthread(ThreadFunc2,0,NULL);

Sleep(3000);

cout<<"end"<

return0;

}

实验二

exa2.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.0;

}

DWORDWINAPIFunTwo(LPVOIDparam){

while(true)

{

Sleep(1000);

cout<<"world!

";

}

return0;

}

intmain(intargc,char*argv[])

{

intinput=0;

HANDLE>>input;

if(input==1)

{

ResumeThread(0;

}

实验三

exa3.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.0;

}

intmain()

{

HANDLE0;

}

实验四:

exa4.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.(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.[2];

CRITICAL_SECTIONcs;

voidWithdrawThread1(LPVOIDparam)

{

EnterCriticalSection(&cs);

if(total-90>=0)

{

total-=90;

cout<<"Youwithdraw90"<

}

else

cout<<"Youdonot(&cs);

SetEvent(evFin[0]);

}

voidWithdrawThread2(LPVOIDparam)

{

EnterCriticalSection(&cs);

if(total-20>=0)

{

total-=20;

cout<<"Youwithdraw20"<

}

else

cout<<"Youdonot(&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.Mutexerror!

"<

}

cout<<"ThreadProc"<

ReleaseMutex(0;

}

intmain(intargc,char*argv[])

{

inti;

DWORDID[THREAD_INSTANCE_NUMBER];

HANDLE0;

}

}

for(i=0;i

{

0;

}

实验七

exa7.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.Semaphoreerror!

"<

}

cout<<"foo"<

ReleaseSemaphore(0;

}

intmain(intargc,char*argv[])

{

inti;

DWORDThreadID[THREAD_INSTANCE_NUMBER];

HANDLE0;

}

for(i=0;i

{

0;

}

实验八:

exa8.cpp:

Definestheclassbehaviorsfortheapplication.

#include"stdafx.theseblocksofgeneratedcode!

}}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:

addconstructioncodeinInitInstance

}

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;

Theoneandonlywindowinitialized,soshowandupdateit.

m_pMainWnd->ShowWindow(SW_SHOW);

m_pMainWnd->UpdateWindow();

returnTRUE;

}

CAboutDlgdialogusedforAppAbout

classCAboutDlg:

publicCDialog

{

public:

CAboutDlg();

DialogData

{{AFX_DATA(CAboutDlg)

enum{IDD=IDD_ABOUTBOX};

}}AFX_DATA

ClassWizardgeneratedvirtualfunctionoverrides

{{AFX_VIRTUAL(CAboutDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport

}}AFX_VIRTUAL

Implementation

protected:

{{AFX_MSG(CAboutDlg)

Nomessagethedialog

voidCExa8App:

:

OnAppAbout()

{

CAboutDlgaboutDlg;

aboutDlg.DoModal();

}

CExa8Appmessage()

{

ThreadStartthreadDelegate=newThreadStart(Work.DoWork);

ThreadnewThread=newThread(threadDelegate);

newThread.Start();

Workw=newWork();

w.Data=42;

threadDelegate=newThreadStart(w.DoMoreWork);

newThread=newThread(threadDelegate);

newThread.Start();

}

}

classWork

{

publicstaticvoidDoWork()

{

Console.WriteLine("Staticthreadprocedure.");

}

publicintData;

publicvoidDoMoreWork()

{

Console.WriteLine("Instancethreadprocedure.Data={0}",Data);

}

}

实验十:

usingSystem;

usingSystem.Threading;

classTest

{

staticinttotal=100;

publicstaticvoidWithDraw1()

{

intn=90;

if(n<=total)

{

total-=n;

Console.WriteLine("You.n={0}",n);

Console.WriteLine("total={0}",total);

}

else

{

Console.WriteLine("Youdonotenoughmoney.n={0}",n);

Console.WriteLine("total={0}",total);

}

}

publicstaticvoidWithDraw2()

{

intn=20;

if(n<=total)

{

total-=n;

Console.WriteLine("You.n={0}",n);

Console.WriteLine("total={0}",total);

}

else

{

Console.WriteLine("Youdonotenoughmoney.n={0}",n);

Console.WriteLine("total={0}",total);

}

}

publicstaticvoidMain()

{

ThreadStartthread1=newThreadStart(WithDraw1);

ThreadnewThread1=newThread(thread1);

ThreadStartthread2=newThreadStart(WithDraw2);

ThreadnewThread2=newThread(thread2);

newThread1.Start();

newThread2.Start();

}

}

实验十一:

exa11.cpp:

Definestheentrypointfortheconsoleapplication.

#include"stdafx.",ThreadNumberTemp);

EnterCriticalSection(&cs);

printf("ThreadProc1%dentersintocriticalsection\n",ThreadNumberTemp);

Sleep(1000);

LeaveCriticalSection(&cs);

return0;

}

DWORDThreadProc2(void*pData){

intThreadNumberTemp=(*(int*)pData);

printf("ThreadProc2:

%disrunning!

\n",ThreadNumberTemp);

EnterCriticalSection(&cs);

printf("ThreadProc2%dentersintocriticalsection\n",ThreadNumberTemp);

Sleep(1000);

LeaveCriticalSection(&cs);

return0;

}

intmain(intargc,char*argv[])

{

inti;

DWORDID1,ID2;

HANDLE(&cs);

printf("Createthecriticalsection\n");

",ID1);

else

printf("CreateThread%d\n",ID1);

",ID2);

else

printf("CreateThread%d\n",ID2);

WaitForSingleObject(\n");

DeleteCriticalSection(&cs);

getch();

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1