做为一名分布式服务器开发人员,在服务器开发领域、多线程开发和并发编程方面有自己的心得和经验,愿意分享给同仁,今讨论下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。