1、多核编程与并行计算实验报告 1(此文档为word格式,下载后您可任意编辑修改!)多核编程与并行计算实验报告 姓名: 日期:2014年 4月20日实验一 exa1.cpp : Defines the entry point for the console application.#include stdafx.() int i=0; _beginthread(ThreadFunc1,0,NULL); _beginthread(ThreadFunc2,0,NULL); Sleep(3000); coutendendl; return 0;实验二 exa2.cpp : Defines the ent
2、ry point for the console application.#include stdafx. 0; DWORD WINAPI FunTwo(LPVOID param) while(true) Sleep(1000); coutinput; if(input=1) ResumeThread( 0;实验三 exa3.cpp : Defines the entry point for the console application.#include stdafx. 0;int main() HANDLE 0;实验四: exa4.cpp : Defines the entry point
3、 for the console application.#include stdafx.(int argc , 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) ; coutThe P
4、rogram is End= 0) total -= 90 ; coutYou withdraw 90endl; else cout= 0) total -= 20 ; coutYou withdraw 20endl; else coutYou do not (&cs) ; LeaveCriticalSection(&cs) ; SetEvent (evFin1) ;int main(int argc , char * argv) evFin0 = CreateEvent (NULL,FALSE,FALSE,NULL) ; evFin1 = CreateEvent (NULL,FALSE,FA
5、LSE,NULL) ; InitializeCriticalSection(&cs) ; _beginthread(WithdrawThread1 , 0 , NULL) ; _beginthread(WithdrawThread2 , 0 , NULL) ; WaitForMultipleObjects(2 ,evFin ,TRUE ,INFINITE) ; DeleteCriticalSection(&cs) ; couttotalendl; return 0 ;实验六: exa6.cpp : Defines the entry point for the console applicat
6、ion.#include stdafx. Mutex error! endl; cout ThreadProc ThreadNumberTemp gets the mutex endl; ReleaseMutex( 0;int main(int argc, char* argv) int i; DWORD IDTHREAD_INSTANCE_NUMBER; HANDLE 0; for (i=0;iTHREAD_INSTANCE_NUMBER;i+) 0;实验七 exa7.cpp : Defines the entry point for the console application.#inc
7、lude stdafx. Semaphore error! endl; cout foo ThreadNumberTemp gets the semaphore endl; ReleaseSemaphore( 0;int main(int argc, char* argv) int i; DWORD ThreadIDTHREAD_INSTANCE_NUMBER; HANDLE 0; for (i=0;iShowWindow(SW_SHOW); m_pMainWnd-UpdateWindow(); return TRUE; CAboutDlg dialog used for App Aboutc
8、lass CAboutDlg : public CDialogpublic: CAboutDlg(); Dialog Data AFX_DATA(CAboutDlg) enum IDD = IDD_ABOUTBOX ; AFX_DATA ClassWizard generated virtual function overrides AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); DDXDDV support AFX_VIRTUAL Implementationprotecte
9、d: AFX_MSG(CAboutDlg) No message the dialogvoid CExa8App:OnAppAbout() CAboutDlg aboutDlg; aboutDlg.DoModal(); CExa8App message () ThreadStart threadDelegate = new ThreadStart(Work.DoWork); Thread newThread = new Thread(threadDelegate); newThread.Start(); Work w = new Work(); w.Data = 42; threadDeleg
10、ate = new ThreadStart(w.DoMoreWork); newThread = new Thread(threadDelegate); newThread.Start(); class Work public static void DoWork() Console.WriteLine(Static thread procedure.); public int Data; public void DoMoreWork() Console.WriteLine(Instance thread procedure. Data=0, Data); 实验十:using System;u
11、sing System.Threading;class Test static int total = 100; public static void WithDraw1() int n=90; if (n = total) total -= n; Console.WriteLine(You . n=0, n); Console.WriteLine(total=0, total); else Console.WriteLine(You do not enough money. n=0, n); Console.WriteLine(total=0, total); public static v
12、oid WithDraw2() int n = 20; if (n = total) total -= n; Console.WriteLine(You . n=0, n); Console.WriteLine(total=0, total); else Console.WriteLine(You do not enough money. n=0, n); Console.WriteLine(total=0, total); public static void Main() ThreadStart thread1 = new ThreadStart(WithDraw1); Thread ne
13、wThread1 = new Thread(thread1); ThreadStart thread2 = new ThreadStart(WithDraw2); Thread newThread2 = new Thread(thread2); newThread1.Start(); newThread2.Start(); 实验十一: exa11.cpp : Defines the entry point for the console application.#include stdafx.,ThreadNumberTemp ); EnterCriticalSection(&cs); pri
14、ntf(ThreadProc1 %d enters into critical sectionn,ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); return 0;DWORD ThreadProc2(void * pData) int ThreadNumberTemp = (*(int*) pData); printf(ThreadProc2: %d is running!n,ThreadNumberTemp ); EnterCriticalSection(&cs); printf(ThreadProc2 %d enters
15、 into critical sectionn,ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); return 0;int main(int argc, char* argv) int i; DWORD ID1,ID2; HANDLE (&cs); printf(Create the critical section n); ,ID1); else printf(CreateThread %d n,ID1); ,ID2); else printf(CreateThread %d n,ID2); WaitForSingleObject ( n); DeleteCriticalSection(&cs); getch(); return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1