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

更新时间:2023-06-06 07:39:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

多核编程与并行计算

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

姓名:

日期:2014年 4月20日

实验一

// exa1.cpp : Defines the entry point for the console application.

多核编程与并行计算

//

#include "stdafx.h"

#include <windows.h>

#include <process.h>

#include <iostream>

#include <fstream>

using namespace std;

void ThreadFunc1(PVOID param)

{

}

void ThreadFunc2(PVOID param)

{

}

int main()

{

}

int i=0; _beginthread(ThreadFunc1,0,NULL); _beginthread(ThreadFunc2,0,NULL); Sleep(3000); cout<<"end"<<endl; return 0; while(1) { } Sleep(1000); cout<<"This is ThreadFunc2"<<endl; while(1) { } Sleep(1000); cout<<"This is ThreadFunc1"<<endl;

多核编程与并行计算

实验二

// exa2.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

#include <iostream>

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 <windows.h>

#include <iostream>

using namespace std;

int globalvar = false;

DWORD WINAPI ThreadFunc(LPVOID pParam)

{

}

int main()

{

}

HANDLE hthread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL); if (!hthread) { } while (!globalvar) cout<<"Thread while"<<endl; cout<<"Thread exit"<<endl; return 0; cout<<"Thread Create Error ! "<<endl; CloseHandle(hthread); cout<<"ThreadFunc"<<endl; Sleep(200); globalvar = true; return 0;

多核编程与并行计算

实验四:

// exa4.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include <windows.h>

#include <process.h>

#include <iostream>

#include <fstream>

using namespace std;

HANDLE evRead, evFinish;

void ReadThread(LPVOID param)

{

}

void WriteThread(LPVOID param)

{

}

int main(int argc , char * argv[])

{ cout<<"Writing"<<endl; SetEvent (evRead); WaitForSingleObject (evRead ,INFINITE); cout<<"Reading"<<endl; SetEvent (evFinish);

多核编程与并行计算

}

evFinish = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ; _beginthread(ReadThread , 0 , NULL) ; _beginthread(WriteThread , 0 , NULL) ; WaitForSingleObject (evFinish,INFINITE) ; cout<<"The Program is End"<<endl; return 0 ;

实验五

// exa5.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

#include <process.h>

#include <iostream>

#include <fstream>

using namespace std;

int total = 100 ;

HANDLE evFin[2] ;

CRITICAL_SECTION cs ;

void WithdrawThread1(LPVOID param)

多核编程与并行计算

}

void WithdrawThread2(LPVOID param)

{

}

int main(int argc , 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<<total<<endl; return 0 ; EnterCriticalSection(&cs) ; if ( total-20 >= 0) { } else cout<<"You do not have that much money"<<endl; LeaveCriticalSection(&cs) ; LeaveCriticalSection(&cs) ; SetEvent (evFin[1]) ; total -= 20 ; cout<<"You withdraw 20"<<endl; EnterCriticalSection(&cs) ; if ( total-90 >= 0) { } else cout<<"You do not have that much money"<<endl; LeaveCriticalSection(&cs) ; SetEvent (evFin[0]) ; total -= 90 ; cout<<"You withdraw 90"<<endl;

多核编程与并行计算

实验六:

// exa6.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include <windows.h>

#include <iostream.h>

#define THREAD_INSTANCE_NUMBER 3

LONG g_fResourceInUse = FALSE;

LONG g_lCounter = 0;

DWORD ThreadProc(void * pData) {

cout << "ThreadProc: " << ThreadNumberTemp << " is running!" << endl; if ((hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Mutex.Test")) == NULL) { } cout << "ThreadProc " << ThreadNumberTemp << " gets the mutex"<< endl; ReleaseMutex(hMutex); cout << "Open Mutex error!" << endl; int ThreadNumberTemp = (*(int*) pData); HANDLE hMutex;

多核编程与并行计算

}

CloseHandle(hMutex); return 0;

int main(int argc, char* argv[])

{

}

return 0; WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE); cout << "Close the Mutex Handle! " << endl; CloseHandle(hMutex); } cout << "CreateThread error" << ID[i] << endl; cout << "CreateThread: " << ID[i] << endl; else h[i] = CreateThread(NULL, if (h[i] == NULL) 0, (LPTHREAD_START_ROUTINE) ThreadProc, (void *)&ID[i], 0, &(ID[i])); for (i=0;i<THREAD_INSTANCE_NUMBER;i++) { int i; DWORD ID[THREAD_INSTANCE_NUMBER]; HANDLE hMutex; if ( (hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Mutex.Test")) == NULL) { } if ((hMutex = CreateMutex(NULL, FALSE, "Mutex.Test")) == NULL ) { } cout << "Create Mutex error!" << endl; return 0; HANDLE h[THREAD_INSTANCE_NUMBER];

多核编程与并行计算

实验七

// exa7.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include <windows.h>

#include <iostream.h>

#define THREAD_INSTANCE_NUMBER 3

DWORD foo(void * pData) {

}

int main(int argc, char* argv[])

{

cout << "foo: " << ThreadNumberTemp << " is running!" << endl; if ((hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, "Semaphore.Test")) == } cout << "foo " << ThreadNumberTemp << " gets the semaphore"<< endl; ReleaseSemaphore(hSemaphore, 1, NULL); CloseHandle(hSemaphore); return 0; cout << "Open Semaphore error!" << endl; int ThreadNumberTemp = (*(int*) pData); HANDLE hSemaphore; NULL) {

多核编程与并行计算

}

int i; DWORD ThreadID[THREAD_INSTANCE_NUMBER]; HANDLE hSemaphore; if ((hSemaphore = CreateSemaphore(NULL,0,1, "Semaphore.Test")) == NULL ) { } cout << "Create Semaphore error!" << endl; return 0; HANDLE hThread[THREAD_INSTANCE_NUMBER]; for (i=0;i<THREAD_INSTANCE_NUMBER;i++) { } WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE); cout << "Close the Semaphore Handle! " << endl; CloseHandle(hSemaphore); return 0; hThread[i] = CreateThread(NULL, if (hThread[i] == NULL) cout << "CreateThread error" << ThreadID[i] << endl; cout << "CreateThread: " << ThreadID[i] << endl; 0, (LPTHREAD_START_ROUTINE) foo, (void *)&ThreadID[i], 0, &(ThreadID[i])); else

多核编程与并行计算

实验八:

// exa8.cpp : Defines the class behaviors for the application.

//

#include "stdafx.h"

#include "exa8.h"

#include "MainFrm.h"

#include "exa8Doc.h"

#include "exa8View.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

///////////////////////////////////////////////////////////////////////////// // CExa8App

BEGIN_MESSAGE_MAP(CExa8App, CWinApp)

//{{AFX_MSG_MAP(CExa8App) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands

多核编程与并行计算

ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CExa8App construction

CExa8App::CExa8App()

{

}

///////////////////////////////////////////////////////////////////////////// // The one and only CExa8App object

CExa8App theApp;

///////////////////////////////////////////////////////////////////////////// // CExa8App initialization

BOOL CExa8App::InitInstance()

{

#ifdef _AFXDLL

// Register the application's document templates. Document templates LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. AfxEnableControlContainer(); // TODO: add construction code here, // Place all significant initialization in InitInstance

多核编程与并行计算

}

CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CExa8Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CExa8View)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE;

///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About

class CAboutDlg : public CDialog

{

public:

// Dialog Data

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA CAboutDlg();

多核编程与并行计算

// Implementation

protected:

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

// App command to run the dialog

void CExa8App::OnAppAbout()

{

}

///////////////////////////////////////////////////////////////////////////// // CExa8App message handlers

CAboutDlg aboutDlg; aboutDlg.DoModal(); //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() END_MESSAGE_MAP()

多核编程与并行计算

、实验九:

using System;

using System.Threading;

class Test

{

}

class Work

{

public static void DoWork() { } public int Data; public void DoMoreWork() { Console.WriteLine("Instance thread procedure. Data={0}", Data); Console.WriteLine("Static thread procedure."); static void Main() { } ThreadStart threadDelegate = new ThreadStart(Work.DoWork); Thread newThread = new Thread(threadDelegate); newThread.Start(); Work w = new Work(); w.Data = 42; threadDelegate = new ThreadStart(w.DoMoreWork); newThread = new Thread(threadDelegate); newThread.Start();

多核编程与并行计算

}

实验十:

using System;

using System.Threading;

class Test

{

} public static void WithDraw2() static int total = 100; public static void WithDraw1() { int n=90; if (n <= total) { } else { } Console.WriteLine("You do not enough money. n={0}", n); Console.WriteLine("total={0}", total); total -= n; Console.WriteLine("You have withdrawn. n={0}", n); Console.WriteLine("total={0}", total);

多核编程与并行计算

}

} public static void Main() { } ThreadStart thread1 = new ThreadStart(WithDraw1); Thread newThread1 = new Thread(thread1); ThreadStart thread2 = new ThreadStart(WithDraw2); Thread newThread2 = new Thread(thread2); newThread1.Start(); newThread2.Start(); int n = 20; if (n <= total) { } else { } Console.WriteLine("You do not enough money. n={0}", n); Console.WriteLine("total={0}", total); total -= n; Console.WriteLine("You have withdrawn. n={0}", n); Console.WriteLine("total={0}", total);

多核编程与并行计算

实验十一:

// exa11.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include <windows.h>

#include <conio.h>

#include <stdio.h>

#define THREAD_INSTANCE_NUMBER 3

LONG g_fResourceInUse = FALSE;

LONG g_lCounter = 0;

CRITICAL_SECTION cs;

DWORD ThreadProc1(void * pData) {

return 0; EnterCriticalSection(&cs); printf("ThreadProc1 %d enters into critical section\n",ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); int ThreadNumberTemp = (*(int*) pData); printf("ThreadProc1: %d is running!\n",ThreadNumberTemp );

多核编程与并行计算

DWORD ThreadProc2(void * pData) {

}

int main(int argc, char* argv[]) {

h2= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc2, (void *)&ID2, 0, &(ID2)); int i; DWORD ID1,ID2; InitializeCriticalSection(&cs); printf("Create the critical section \n"); h1 = CreateThread(NULL, printf("CreateThread error %d \n",ID1); printf("CreateThread %d \n",ID1); 0, (LPTHREAD_START_ROUTINE) ThreadProc1, (void *)&ID1, 0, &(ID1)); return 0; EnterCriticalSection(&cs); printf("ThreadProc2 %d enters into critical section\n",ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); int ThreadNumberTemp = (*(int*) pData); printf("ThreadProc2: %d is running!\n",ThreadNumberTemp ); HANDLE h1,h2; if (h1 == NULL) else

本文来源:https://www.bwwdw.com/article/li11.html

Top