| 主機(jī)參考:VPS測評參考推薦/專注分享VPS服務(wù)器優(yōu)惠信息!若您是商家可以在本站進(jìn)行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務(wù),查看詳情! |
| 我們發(fā)布的部分優(yōu)惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關(guān)文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
做為一名分布式服務(wù)器開發(fā)人員,在服務(wù)器開發(fā)領(lǐng)域、多線程開發(fā)和并發(fā)編程方面有自己的心得和經(jīng)驗,愿意分享給同仁,今討論下Windows下線程并發(fā)程序開發(fā)。
?
下面用用兩個線程實現(xiàn)一個簡單的數(shù)組排序,演示了線程的基本用法。
?
?
原理是:
為了節(jié)省執(zhí)行時間而添加并行,把問題劃分為幾個小問題,并分配給幾個線程(分而治之),把問題劃分成若干更小的單元,更容易在實現(xiàn)中創(chuàng)建并行邏輯。同時,在并行中使用系統(tǒng)資源能優(yōu)化應(yīng)用程序并提高其運(yùn)行速度。
#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++;}}}
運(yùn)行結(jié)果:
中間在編譯運(yùn)行的時候會遇到一些細(xì)節(jié)問題,特此說明:
出現(xiàn)錯誤:
?錯誤C1189#error:??"No?Target?Architecture"
設(shè)置如下即可:在_X86_
?
分布式開發(fā),服務(wù)器開發(fā),多線程開發(fā),并發(fā)程序設(shè)計,任重而道遠(yuǎn)。
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.
--------------------------------------------------------------
主機(jī)參考,收集國內(nèi)外VPS,VPS測評,主機(jī)測評,云服務(wù)器,虛擬主機(jī),獨(dú)立服務(wù)器,國內(nèi)外服務(wù)器,高性價比建站主機(jī)相關(guān)優(yōu)惠信息@m.k2270.com
詳細(xì)介紹和測評國外VPS主機(jī),云服務(wù)器,國外服務(wù)器,國外主機(jī)的相關(guān)優(yōu)惠信息,商家背景,網(wǎng)絡(luò)帶寬等等,也是目前國內(nèi)最好的主機(jī)云服務(wù)器VPS參考測評資訊優(yōu)惠信息分享平臺
這幾篇文章你可能也喜歡:
- Digitalocean:悉尼數(shù)據(jù)中心開放,悉尼計算機(jī)室的評估(悉尼分區(qū)簡介)
- 磚瓦家:香港CN2 GIA VPS庫存,2核2G高速VPS $ 89
- EVOXT荷蘭VPS評論:CPU硬盤性能很強(qiáng),3個網(wǎng)絡(luò)直接連接,60,000個視頻 +速度良好
- 美國紐約紐約的Server Host 10GBPS帶寬無限流量專用服務(wù)器托管:從$ 265/月起
- 主機(jī)評測 VMISS 日本BGP專線VPS套餐 JP.TKY.BGP.基本,國內(nèi)直連,低延遲,寬帶(日本VPS哪家好?)
本文由主機(jī)參考刊發(fā),轉(zhuǎn)載請注明:Windows多線程開發(fā)之并發(fā)線程程序研究 http://m.k2270.com/12796.html
主機(jī)參考
















評論前必須登錄!
注冊