多核编程与并行计算实验报告 1.docx
《多核编程与并行计算实验报告 1.docx》由会员分享,可在线阅读,更多相关《多核编程与并行计算实验报告 1.docx(16页珍藏版)》请在冰豆网上搜索。
多核编程与并行计算实验报告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"<}elsecout<<"Youdonot(&cs);SetEvent(evFin[0]);}voidWithdrawThread2(LPVOIDparam){EnterCriticalSection(&cs);if(total-20>=0){total-=20;cout<<"Youwithdraw20"<}elsecout<<"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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
return0;
}
实验二
exa2.cpp:
#include"stdafx.0;
DWORDWINAPIFunTwo(LPVOIDparam){
while(true)
Sleep(1000);
cout<<"world!
";
intmain(intargc,char*argv[])
intinput=0;
HANDLE>>input;
if(input==1)
ResumeThread(0;
实验三
exa3.cpp:
intmain()
HANDLE0;
实验四:
exa4.cpp:
#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"<}elsecout<<"Youdonot(&cs);SetEvent(evFin[0]);}voidWithdrawThread2(LPVOIDparam){EnterCriticalSection(&cs);if(total-20>=0){total-=20;cout<<"Youwithdraw20"<}elsecout<<"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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
实验五
exa5.cpp:
#include"stdafx.[2];
CRITICAL_SECTIONcs;
voidWithdrawThread1(LPVOIDparam)
EnterCriticalSection(&cs);
if(total-90>=0)
total-=90;
cout<<"Youwithdraw90"<}elsecout<<"Youdonot(&cs);SetEvent(evFin[0]);}voidWithdrawThread2(LPVOIDparam){EnterCriticalSection(&cs);if(total-20>=0){total-=20;cout<<"Youwithdraw20"<}elsecout<<"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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
else
cout<<"Youdonot(&cs);
SetEvent(evFin[0]);
voidWithdrawThread2(LPVOIDparam)
if(total-20>=0)
total-=20;
cout<<"Youwithdraw20"<}elsecout<<"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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
LeaveCriticalSection(&cs);
SetEvent(evFin[1]);
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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
实验六:
exa6.cpp:
#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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
ReleaseMutex(0;
inti;
DWORDID[THREAD_INSTANCE_NUMBER];
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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
0;
实验七
exa7.cpp:
#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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
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_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
ReleaseSemaphore(0;
DWORDThreadID[THREAD_INSTANCE_NUMBER];
for(i=0;i{0;}实验八:exa8.cpp:Definestheclassbehaviorsfortheapplication.#include"stdafx.theseblocksofgeneratedcode!}}AFX_MSG_MAPStandardfilebaseddocumentcommandsON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)END_MESSAGE_MAP()CExa8AppconstructionCExa8App::CExa8App(){TODO:addconstructioncodeinInitInstance}TheoneandonlyCExa8AppobjectCExa8ApptheApp;CExa8AppinitializationBOOLCExa8App::InitInstance(){AfxEnableControlContainer();StandardinitializationIfyouarenotusingthesefeaturesandwishtoreducethesizeofyourfinalexecutable,youshouldremovefromthefollowingthespecificinitializationroutinesyoudonotneed.#ifdef_AFXDLLEnable3dControls();CallthiswhenusingMFCinasharedDLL#elseEnable3dControlsStatic();CallthiswhenlinkingtoMFCstatically#endifChangetheregistrykeyunderwhichoursettingsarestored.TODO:Youshouldmodifythisstringtobesomethingappropriatesuchasthenameofyourcompanyororganization.SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));LoadStdProfileSettings();LoadstandardINIfileoptions(includingMRU)Registertheapplication'sdocumenttemplates.Documenttemplatesserveastheconnectionbetweendocuments,framewindowsandviews.CSingleDocTemplate*pDocTemplate;pDocTemplate=newCSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CExa8Doc),RUNTIME_CLASS(CMainFrame),mainSDIframewindowRUNTIME_CLASS(CExa8View));AddDocTemplate(pDocTemplate);Parsecommandlineforstandardshellcommands,DDE,fileopenCCommandLineInfocmdInfo;ParseCommandLine(cmdInfo);Dispatchcommandsspecifiedonthecommandlineif(!ProcessShellCommand(cmdInfo))returnFALSE;Theoneandonlywindowinitialized,soshowandupdateit.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();returnTRUE;}CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg();DialogData{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};}}AFX_DATAClassWizardgeneratedvirtualfunctionoverrides{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);DDXDDVsupport}}AFX_VIRTUALImplementationprotected:{{AFX_MSG(CAboutDlg)NomessagethedialogvoidCExa8App::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);elseprintf("CreateThread%d\n",ID1);",ID2);elseprintf("CreateThread%d\n",ID2);WaitForSingleObject(\n");DeleteCriticalSection(&cs);getch();return0;}
实验八:
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.
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
{{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);
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);
Console.WriteLine("Youdonotenoughmoney.n={0}",n);
publicstaticvoidWithDraw2()
intn=20;
publicstaticvoidMain()
ThreadStartthread1=newThreadStart(WithDraw1);
ThreadnewThread1=newThread(thread1);
ThreadStartthread2=newThreadStart(WithDraw2);
ThreadnewThread2=newThread(thread2);
newThread1.Start();
newThread2.Start();
实验十一:
exa11.cpp:
#include"stdafx.",ThreadNumberTemp);
printf("ThreadProc1%dentersintocriticalsection\n",ThreadNumberTemp);
DWORDThreadProc2(void*pData){
intThreadNumberTemp=(*(int*)pData);
printf("ThreadProc2:
%disrunning!
\n",ThreadNumberTemp);
printf("ThreadProc2%dentersintocriticalsection\n",ThreadNumberTemp);
DWORDID1,ID2;
HANDLE(&cs);
printf("Createthecriticalsection\n");
",ID1);
printf("CreateThread%d\n",ID1);
",ID2);
printf("CreateThread%d\n",ID2);
WaitForSingleObject(\n");
getch();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1