您好,登錄后才能下訂單哦!
做為一名分布式服務器開發人員,在服務器開發領域、多線程開發和并發編程方面有自己的心得和經驗,愿意分享給同仁,今討論下Windows下線程并發程序開發。
下面用用兩個線程實現一個簡單的數組排序,演示了線程的基本用法。
原理是:
為了節省執行時間而添加并行,把問題劃分為幾個小問題,并分配給幾個線程(分而治之),把問題劃分成若干更小的單元,更容易在實現中創建并行邏輯。同時,在并行中使用系統資源能優化應用程序并提高其運行速度。
#include "stdafx.h" #include <iostream> #include <Windows.h> #include <Winternl.h> #include <tchar.h> #include <winbase.h> using namespace std; #define THREADS_NUMBER 2 #define ELEMENTS_NUMBER 200 #define BLOCK_SIZE ELEMENTS_NUMBER / THREADS_NUMBER #define MAX_VALUE 1000 typedef struct _tagARRAYOBJECT { int* iArray; int iSize; int iThreadID; } ARRAYOBJECT, *PARRAYOBJECT; DWORD WINAPI ThreadStart( LPVOID lpParameter); void PrintArray( int* iArray, int iSize); void MergeArrays(int* leftArray, int leftArrayLenght, int* rightArray, int rightArrayLenght, int* mergedArray); int _tmain(int argc, _TCHAR* argv[]) { int iArray1[BLOCK_SIZE]; int iArray2[BLOCK_SIZE]; int iArray[ELEMENTS_NUMBER]; for (int iIndex = 0; iIndex < BLOCK_SIZE; iIndex++) { iArray1[iIndex] = rand() % MAX_VALUE; iArray2[iIndex] = rand() % MAX_VALUE; } HANDLE hThreads[THREADS_NUMBER]; ARRAYOBJECT pObject1 = { &(iArray1[0]), BLOCK_SIZE, 0 }; hThreads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadStart,(LPVOID)& pObject1, 0, NULL); ARRAYOBJECT pObject2 = { &(iArray2[0]), BLOCK_SIZE, 1 }; hThreads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadStart, (LPVOID)& pObject2, 0, NULL);cout<<"Waiting execution..." << endl; WaitForMultipleObjects(THREADS_NUMBER, hThreads, TRUE, INFINITE); MergeArrays(&iArray1[0], BLOCK_SIZE, &iArray2[0], BLOCK_SIZE, &iArray[0]); PrintArray(iArray, ELEMENTS_NUMBER); CloseHandle(hThreads[0]); CloseHandle(hThreads[1]); cout<< "Array sorted..." << endl; getchar(); return 0; } DWORD WINAPI ThreadStart(LPVOID lpParameter) { PARRAYOBJECT pObject = (PARRAYOBJECT)lpParameter; int iTmp = 0; for (int iIndex = 0; iIndex < pObject->iSize; iIndex++) { for (int iEndIndex = pObject->iSize - 1; iEndIndex > iIndex; iEndIndex--) { if (pObject->iArray[iEndIndex] < pObject->iArray[iIndex]) { iTmp = pObject->iArray[iEndIndex]; pObject->iArray[iEndIndex] = pObject->iArray[iIndex]; pObject->iArray[iIndex] = iTmp; } } } return 0; } void PrintArray(int* iArray, int iSize) { for (int iIndex = 0; iIndex < iSize; iIndex++) { cout << " " << iArray[iIndex]; } cout << endl; } void MergeArrays(int* leftArray, int leftArrayLenght, int* rightArray, int rightArrayLenght, int* mergedArray) { int i = 0; int j = 0; int k = 0; while (i < leftArrayLenght && j < rightArrayLenght) { if (leftArray[i] < rightArray[j]) { mergedArray[k] = leftArray[i]; i++; } else { mergedArray[k] = rightArray[j]; j++; } k++; } if (i >= leftArrayLenght) { while (j < rightArrayLenght) { mergedArray[k] = rightArray[j]; j++; k++; } } if (j >= rightArrayLenght) { while (i < leftArrayLenght) { mergedArray[k] = leftArray[i]; i++; k++; } } }
運行結果:
中間在編譯運行的時候會遇到一些細節問題,特此說明:
出現錯誤:
錯誤 C1189 #error: "No Target Architecture"
設置如下即可:在_X86_
分布式開發,服務器開發,多線程開發,并發程序設計,任重而道遠。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。