Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Research on concurrent Thread Program for Windows Multithreading Development

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

As a distributed server developer, I have my own experience and experience in server development, multi-thread development and concurrent programming. I would like to share it with my colleagues. I would like to discuss thread concurrent program development under Windows.

The following demonstrates the basic use of threads by implementing a simple array sorting with two threads.

The principle is:

To save execution time, add parallelism, divide the problem into several small problems and assign them to several threads (divide and conquer), divide the problem into several smaller units, and make it easier to create parallel logic in the implementation. At the same time, the use of system resources in parallel can optimize the application and improve its running speed.

# include "stdafx.h" # include # include 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 iArray 1 [block _ SIZE]; int iArray 2 [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);coutiArray[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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report