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

What is the method of implementing thread pool with Cracket + Native API?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what is the method of implementing thread pool with Cpicurus + native API". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of implementing thread pool with CAccord Craft + Native API"?

Thread pool has two core concepts, one is the task queue and the other is the worker thread queue. The task queue is responsible for storing the tasks that the main thread needs to deal with, and the worker thread queue is actually an endless cycle, which is responsible for fetching and running tasks from the task queue, which can be regarded as a model of a producer and multiple consumers. Thread pool is also a common technology in some highly concurrent network applications. The C++ multithreading server programming mode recommended by Chen Shuo is: one loop per thread + thread pool. Usually, a separate thread is responsible for receiving requests from the client, parsing the requests and submitting the data processing tasks to a special computing thread pool.

ThreadPool thread pool synchronization events: thread functions in the thread pool also support mutex, signal control, kernel event control, critical section control.

# include # include # include unsigned long g_count = 0 / /-- / Thread pool synchronization-Mutex synchronization void NTAPI TaskHandlerMutex (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) {/ / Lock resource WaitForSingleObject (* (HANDLE *) Context, INFINITE) For (int x = 0; x

< 100; x++) { printf("线程ID: %d --->

Child thread:% d\ n ", GetCurrentThreadId (), x); g_count = g_count + 1;} / / unlock resource ReleaseMutexWhenCallbackReturns (Instance, * (HANDLE*) Context);} void TestMutex () {/ / create mutex HANDLE hMutex = CreateMutex (NULL, FALSE, NULL); PTP_WORK pool = CreateThreadpoolWork ((PTP_WORK_CALLBACK) TaskHandlerMutex, & hMutex, NULL) For (int I = 0; I

< 1000; i++) { SubmitThreadpoolWork(pool); } WaitForThreadpoolWorkCallbacks(pool, FALSE); CloseThreadpoolWork(pool); CloseHandle(hMutex); printf("相加后 --->

% d\ n ", g_count) } / /-- / Thread pool synchronization-event kernel object void NTAPI TaskHandlerKern (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) {/ / Lock resource WaitForSingleObject (* (HANDLE *) Context, INFINITE) For (int x = 0; x

< 100; x++) { printf("线程ID: %d --->

Child threads:% d\ n ", GetCurrentThreadId (), x); g_count = g_count + 1;} / / unlock resource SetEventWhenCallbackReturns (Instance, * (HANDLE*) Context);} void TestKern () {HANDLE hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); SetEvent (hEvent); PTP_WORK pwk = CreateThreadpoolWork ((PTP_WORK_CALLBACK) TaskHandlerKern, & hEvent, NULL) For (int I = 0; I

< 1000; i++) { SubmitThreadpoolWork(pwk); } WaitForThreadpoolWorkCallbacks(pwk, FALSE); CloseThreadpoolWork(pwk); printf("相加后 --->

% d\ n ", g_count) } / /-- / Thread Pool synchronization-semaphore synchronization void NTAPI TaskHandlerSemaphore (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) {/ / Lock resource WaitForSingleObject (* (HANDLE *) Context, INFINITE) For (int x = 0; x

< 100; x++) { printf("线程ID: %d --->

Child threads:% d\ n ", GetCurrentThreadId (), x); g_count = g_count + 1;} / / unlock resource ReleaseSemaphoreWhenCallbackReturns (Instance, * (HANDLE*) Context, 1);} void TestSemaphore () {/ / create semaphore 100HANDLE hSemaphore = CreateSemaphore (NULL, 0,100, NULL); ReleaseSemaphore (hSemaphore, 10, NULL) PTP_WORK pwk = CreateThreadpoolWork ((PTP_WORK_CALLBACK) TaskHandlerSemaphore, & hSemaphore, NULL); for (int I = 0; I

< 1000; i++) { SubmitThreadpoolWork(pwk); } WaitForThreadpoolWorkCallbacks(pwk, FALSE); CloseThreadpoolWork(pwk); CloseHandle(hSemaphore); printf("相加后 --->

% d\ n ", g_count) } / /-- / Thread pool synchronization-critical section void NTAPI TaskHandlerLeave (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) {/ / Lock resource EnterCriticalSection ((CRITICAL_SECTION*) Context) For (int x = 0; x

< 100; x++) { printf("线程ID: %d --->

Child thread:% d\ n ", GetCurrentThreadId (), x); g_count = g_count + 1;} / / unlock resource LeaveCriticalSectionWhenCallbackReturns (Instance, (CRITICAL_SECTION*) Context);} void TestLeave () {CRITICAL_SECTION cs; InitializeCriticalSection (& cs); PTP_WORK pwk = CreateThreadpoolWork ((PTP_WORK_CALLBACK) TaskHandlerLeave, & cs, NULL) For (int I = 0; I

< 1000; i++) { SubmitThreadpoolWork(pwk); } WaitForThreadpoolWorkCallbacks(pwk, FALSE); DeleteCriticalSection(&cs); CloseThreadpoolWork(pwk); printf("相加后 --->

% d\ n ", g_count);} int main (int argc,char * argv) {/ / TestMutex (); / / TestKern (); / / TestSemaphore (); TestLeave (); system (" pause "); return 0;}

Simple IO read and write:

# include # include # include / / simple asynchronous text reading and writing int ReadWriteIO () {char enContent [] = "hello lyshark"; char deContent [255] = {0}; / / asynchronous writing file HANDLE hFileWrite = CreateFile (L "d://test.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL) If (INVALID_HANDLE_VALUE = = hFileWrite) {return 0;} WriteFile (hFileWrite, enContent, strlen (enContent), NULL, NULL); FlushFileBuffers (hFileWrite); CancelSynchronousIo (hFileWrite); CloseHandle (hFileWrite); / / Asynchronous read file HANDLE hFileRead = CreateFile (L "d://test.txt", GENERIC_READ, 0, NULL, OPEN_ALWAYS, NULL, NULL) If (INVALID_HANDLE_VALUE = = hFileRead) {return 0;} ReadFile (hFileRead, deContent, 255, NULL, NULL); CloseHandle (hFileRead); std::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

Development

Wechat

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

12
Report