主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情! |
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作 |
做为一名分布式服务器开发人员,在服务器开发领域、多线程开发和并发编程方面有自己的心得和经验,愿意分享给同仁,今讨论下Windows下线程并发程序开发。
下面用用两个线程实现一个简单的数组排序,演示了线程的基本用法。
原理是:
为了节省执行时间而添加并行,把问题划分为几个小问题,并分配给几个线程(分而治之),把问题划分成若干更小的单元,更容易在实现中创建并行逻辑。同时,在并行中使用系统资源能优化应用程序并提高其运行速度。
#include "stdafx.h" #include <iostreamgt;#include <Windows.hgt;#include <Winternl.hgt;#include <tchar.hgt;#include <winbase.hgt;using namespace std;#define THREADS_NUMBER 2#define ELEMENTS_NUMBER 200#define BLOCK_SIZE ELEMENTS_NUMBER / THREADS_NUMBER #define MAX_VALUE 1000typedef 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-gt;iSize; iIndex++){for (int iEndIndex = pObject-gt;iSize - 1; iEndIndex gt; iIndex; iEndIndex--) {if (pObject-gt;iArray[iEndIndex] < pObject-gt;iArray[iIndex]){iTmp = pObject-gt;iArray[iEndIndex];pObject-gt;iArray[iEndIndex] = pObject-gt;iArray[iIndex];pObject-gt;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 gt;= leftArrayLenght){while (j < rightArrayLenght){mergedArray[k] = rightArray[j];j++;k++;}}if (j gt;= rightArrayLenght){while (i < leftArrayLenght){mergedArray[k] = leftArray[i];i++;k++;}}}
运行结果:
中间在编译运行的时候会遇到一些细节问题,特此说明:
出现错误:
错误C1189#error: "No Target Architecture"
设置如下即可:在_X86_
分布式开发,服务器开发,多线程开发,并发程序设计,任重而道远。
As a distributed server developer, I have my own experience in the field of server development, multithreading development and concurrent programming, and would like to share it with my colleagues. Now I will discuss the development of thread concurrent programming under windows.
The following uses two threads to implement a simple array sorting, demonstrating the basic use of threads.
The principle is:
In order to save execution time and add parallelism, the problem is divided into several small problems and allocated to several threads (divide and rule), and the problem is divided into several smaller units, which makes it easier to create parallel logic in the implementation. At the same time, using system resources in parallel can optimize the application and improve its running speed.
#include "stdafx.h" #include <iostreamgt;#include <Windows.hgt;#include <Winternl.hgt;#include <tchar.hgt;#include <winbase.hgt;using namespace std; #define THREADS_NUMBER 2#define ELEMENTS_NUMBER 200#define BLOCK_SIZE ELEMENTS_NUMBER / THREADS_NUMBER #define MAX_VALUE 1000typedef 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-gt;iSize; iIndex++){for (int iEndIndex = pObject-gt;iSize - 1; iEndIndex gt; iIndex; iEndIndex--) {if (pObject-gt;iArray[iEndIndex] < pObject-gt;iArray[iIndex]){iTmp = pObject-gt;iArray[iEndIndex];pObject-gt;iArray[iEndIndex] = pObject-gt;iArray[iIndex];pObject-gt;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 gt;= leftArrayLenght){while (j < rightArrayLenght){mergedArray[k] = rightArray[j];j++;k++;}}if (j gt; = rightArrayLenght){while (i < leftArrayLenght){mergedArray[k] = leftArray[i];i++;k++;}}}
Operation result:
In the process of compiling and running, we will encounter some details, which are as follows:
An error occurred:
Error C1189 error: "no target architecture"
The settings are as follows: in ﹐ x86_
Distributed development, server development, multithreading development and concurrent programming have a long way to go.
--------------------------------------------------------------
主机参考,收集国内外VPS,VPS测评,主机测评,云服务器,虚拟主机,独立服务器,国内外服务器,高性价比建站主机相关优惠信息@zhujicankao.com
详细介绍和测评国外VPS主机,云服务器,国外服务器,国外主机的相关优惠信息,商家背景,网络带宽等等,也是目前国内最好的主机云服务器VPS参考测评资讯优惠信息分享平台
这几篇文章你可能也喜欢:
- 华纳云:双11特价购买,香港CN2云服务器4H16G季付499元,续订价格不变
- 恒天云:双11狂欢大促,云服务器月费12元起,服务器价格低至455元每月
- LuxVPS:德国VPS特价6.99欧元每月起,KVM虚拟/免费DDoS防护
- #double11#edgeNAT:终身VPS价格24元/月起,服务器低至500元
- 搬砖VPS:洛杉矶DC6/DC9机房免费添加CMIN2高端线路,价格不变
本文由主机参考刊发,转载请注明:Windows多线程开发之并发线程程序研究 https://zhujicankao.com/12796.html
评论前必须登录!
注册