Windows多線程開發之並發線程程序研究
阿新 • • 發佈:2017-09-08
分布式開發 服務器開發 多線程開發 c/c++並發程序設計 windows開發
做為一名分布式服務器開發人員,在服務器開發領域、多線程開發和並發編程方面有自己的心得和經驗,願意分享給同仁,今討論下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_
分布式開發,服務器開發,多線程開發,並發程序設計,任重而道遠。
Windows多線程開發之並發線程程序研究