多核编程与并行计算实验报告 (1)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
多核编程与并行计算实验报告
姓名:
日期: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);
cout<<"This is ThreadFunc1"< } } void ThreadFunc2(PVOID param) { while(1) { Sleep(1000); cout<<"This is ThreadFunc2"< } } int main() { int i=0; _beginthread(ThreadFunc1,0,NULL); _beginthread(ThreadFunc2,0,NULL); Sleep(3000); cout<<"end"< return 0; } 实验二 // exa2.cpp : Defines the entry point for the console application. // #include"stdafx.h" #include #include using namespace std; DWORD WINAPI FunOne(LPVOID param){ while(true) { Sleep(1000); cout<<"hello! "; } return 0; } DWORD WINAPI FunTwo(LPVOID param){ while(true) { Sleep(1000); cout<<"world! "; } return 0; } int main(int argc, char* argv[]) { int input=0; HANDLE hand1=CreateThread (NULL, 0, FunOne, (void*)&input, CREATE_SUSPENDED, NULL); HANDLE hand2=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); 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) { cout<<"ThreadFunc"< Sleep(200); globalvar = true; return 0; } int main() { HANDLE hthread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL); if (!hthread) { cout<<"Thread Create Error ! "< CloseHandle(hthread); } while (!globalvar) cout<<"Thread while"< cout<<"Thread exit"< return 0; } 实验四: // exa4.cpp : Defines the entry point for the console application. // #include"stdafx.h" #include #include #include #include using namespace std; HANDLE evRead, evFinish; void ReadThread(LPVOID param) { WaitForSingleObject (evRead ,INFINITE); cout<<"Reading"< SetEvent (evFinish); } void WriteThread(LPVOID param) { cout<<"Writing"< SetEvent (evRead); } int main(int argc , char * argv[]) {