As a distributed server developer, in the field of server development, multi-threaded development and concurrent programming have their own experiences and experience, willing to share to colleagues, today discusses the Windows thread concurrent program development.
The following uses two threads to implement a simple array ordering, demonstrating the basic usage of threads.
The principle is:
To save execution time and add parallelism, divide the problem into several small problems and assign several threads (divide and conquer), divide the problem into smaller units, and make it easier to create parallel logic in the implementation. At the same time, using system resources in parallel optimizes the application and improves its speed of operation.
#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 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->isize; iindex++) {for (int iendindex = pobject->isize - 1; iendindex > iindex; iendindex--) {if (pobject->iarray[ Iendindex] < pobject->iarray[iindex]) {Itmp = pobject->iarray[iendindex];p object- >iarray[iendindex] = pobject->iarray[iindex];p object->iarray[iindex] = itmp;}}} return 0;} void printarray (int* iarray, int isize) {for (int iindex = 0; iindex < isize; iindex++) {cout << " " &Nbsp;<< 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++;}}}
Operation Result:
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/06/19/wKiom1mxTd-DQ4y-AACa7r0JYg0391.png-wh_500x0-wm_ 3-wmp_4-s_37734426.png "title=" 2.png "alt=" Wkiom1mxtd-dq4y-aaca7r0jyg0391.png-wh_50 "/>
In the middle of the compile run will encounter some details of the problem, hereby explained:
Error occurred:
Error C1189#error: "No Target Architecture"
Set the following: in _x86_
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/A4/CA/wKioL1mxTeSAXliRAAJ9zVIB55Y167.png-wh_500x0-wm_ 3-wmp_4-s_2131162818.png "style=" Float:none; "title=" 1.png "alt=" Wkiol1mxtesaxliraaj9zvib55y167.png-wh_50 "/>
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/06/19/wKiom1mxTgXxDcWKAAEhzgQANjM841.png-wh_500x0-wm_ 3-wmp_4-s_1503877052.png "style=" Float:none; "title=" 3.png "alt=" Wkiom1mxtgxxdcwkaaehzgqanjm841.png-wh_50 "/>
Distributed development, server development, multi-threaded development, concurrent programming, any heavy and long way.
Research on concurrent thread programming of Windows multithreading