《多核编程与并行计算实验报告 (1)》由会员分享,可在线阅读,更多相关《多核编程与并行计算实验报告 (1)(22页珍藏版)》请在金锄头文库上搜索。
1、多核编程与并行计算实验报告姓名:日期:2014 年 4月 20 日实验一/ exa1.cpp : Defines the entry point for the console application./ #include stdafx.h#include #include #include #include using namespace std;void ThreadFunc1(PVOID param)while(1)Sleep(1000);coutThis is ThreadFunc1endl;void ThreadFunc2(PVOID param)while(1)Sleep(1000
2、);coutThis is ThreadFunc2endl;int main()int i=0;_beginthread(ThreadFunc1,0,NULL);_beginthread(ThreadFunc2,0,NULL);Sleep(3000);coutendendl;return 0;尅新建 Iicrosoft Tord 文档-Microsoft Tord准局甫围)#include *stdafx. h*#include $#include #include #include S ccSvoid Thre?while1HintmainC)int位置2. 5厘米”五号灵 C: VIllD
3、OVSsyst e32cad. exeThis is ThreadFunc1This is ThreadFunc2This is ThreadFunc2This is ThreadFuncl endThis is ThreadFunclThis is ThreadFunc2请按任意键继续-B 工卫”凶必宜“厦尋言181拒订| !: I I I -19- I -20- I . .21- =begir行 1列英语集国国实验二/ exa2.cpp : Defines the entry point for the console application /#include stdafx.h#incl
4、ude #include using namespace std;DWORD WINAPI FunOne(LPVOID param)while(true)Sleep(1000); couthello! ;return 0;DWORD WINAPI FunTwo(LPVOID param)while(true)Sleep(1000); coutinput;if(input=1)ResumeThread(hand1);ResumeThread(hand2);elseSuspendThread(hand1);SuspendThread(hand2);TerminateThread(hand1,1);
5、TerminateThread(hand2,1);return 0;实验三/ exa3.cpp : Defines the entry point for the console application./#include stdafx.h#include #include using namespace std;int globalvar = false;DWORD WINAPI ThreadFunc(LPVOID pParam)coutThreadFuncendl;Sleep(200);globalvar = true;return 0;int main()HANDLE hthread =
6、 CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);if (!hthread)coutThread Create Error ! endl;CloseHandle(hthread);while (!globalvar)coutThread whileendl;coutThread exitendl;return 0;实验四:/ exa4.cpp : Defines the entry point for the console application. /#includestdafx.h#include#include#include#inclu
7、deusing namespace std;HANDLE evRead, evFinish;void ReadThread(LPVOID param)WaitForSingleObject (evRead ,INFINITE); coutReadingendl;SetEvent (evFinish);void WriteThread(LPVOID param)coutWritingendl;SetEvent (evRead);int main(int argc , char * argv)evRead = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ; evF
8、inish = CreateEvent (NULL ,FALSE ,FALSE ,NULL)_beginthread(ReadThread , 0 , NULL) ; _beginthread(WriteThread , 0 , NULL) ;WaitForSingleObject (evFinish,INFINITE) coutThe Program is Endendl; return 0 ;实验五/ exa5.cpp : Defines the entry point for the console application./#includestdafx.h#include#includ
9、e#include#includeusing namespace std;int total = 100 ;HANDLE evFin2 ;CRITICAL_SECTION cs ;void WithdrawThread1(LPVOID param)EnterCriticalSection(&cs) ;if ( total-90 = 0)total -= 90 ;coutYou withdraw 90endl;elsecoutYou do not have that much money= 0)total -= 20 ;coutYou withdraw 20endl;elsecoutYou do
10、 not have that much moneyendl;LeaveCriticalSection(&cs) ;LeaveCriticalSection(&cs) ;SetEvent (evFin1) ;int main(int argc , char * argv)evFin0 = CreateEvent (NULL,FALSE,FALSE,NULL) ;evFin1 = CreateEvent (NULL,FALSE,FALSE,NULL) ; InitializeCriticalSection(&cs) ;_beginthread(WithdrawThread1 , 0 , NULL)
11、 ;_beginthread(WithdrawThread2 , 0 , NULL) ;WaitForMultipleObjects(2 ,evFin ,TRUE ,INFINITE) ; DeleteCriticalSection(&cs) ;couttotalendl;return 0 ;实验六:/ exa6.cpp : Defines the entry point for the console application./#include stdafx.h#include #include #define THREAD_INSTANCE_NUMBER 3LONG g_fResourceInUse = FALSE;LONG g_lCounter = 0;DWORD ThreadProc(void * pData) int ThreadNumberTemp = (*(int*) pData);HANDLE hMutex;cout ThreadProc: ThreadNumberTemp is running! endl;if (hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, Mutex.Test) = NULL) cout Open Mutex error! endl;cout ThreadProc ThreadNumb