11th Chapter Windows thread Pool (3) _ Private thread pool

Source: Internet
Author: User

11.3 Private thread pool

11.3.1 creating and destroying a private thread pool

(1) Process default thread pool

When calling Createthreadpoolwork, Createthreadpooltimer, createthreadpoolwait, or Createthreadpoolio, and making incoming arguments Ptp_callback_ When Environ is set to null , All work items are added to the process's default thread pool . In general, this default thread pool can meet the requirements of most applications. Its lifetime is the same as the process, and Windows is responsible for cleaning and destroying the thread pool at the end of the process.

(2) Create a private thread pool : Ptp_pool createthreadpool (PVOID reserved); parameter is NULL

(3) destroying the private thread pool: Closethreadpool (Ptp_pool pthreadpool);

11.3.2 Custom private thread pool

(1) Set the maximum and minimum number of threads in thread pool

BOOL setthreadpoolthreadminimum (Ptp_pool pthreadpool,dword cthrdmin);

BOOL setthreadpoolthreadmaximum (Ptp_pool pthreadpool,dword Cthrdmax);

Note the minimum number of default thread pools is 1 and the maximum number is 500

11.3.3 callback environment for thread pool (callback environment)--tp_callback_environ struct

(1) Initialization callback environment: Initializethreadpoolenvironment (Ptp_callback_environ pcbe);

The purpose of the description initialization is to set the version to 1 in the callback environment structure and the remainder to 0.

(2) Associating a callback environment to a specified thread pool

Setthreadpoolcallbackpool (Ptp_callback_environ pcbe,ptp_pool pthreadpool);

"Note" If the function is not called, PBCE is null. The work items added to the thread pool are added to the process's default thread pool. Otherwise, it is added to the thread pool specified by Pthreadpool.

(3) Destruction callback environment: Destroythreadpoolenvironment (Ptp_callback_environ pcbe)

11.3.4 Cleanup Group-gracefully destroy the thread pool

(1) Create cleanup Group: Ptp_cleanup_group Createthreadpoolcleanupgroup ();

(2) Associating a cleanup group with a thread pool: setthreadpoolcallbackcleanupgroup function

Parameters

Describe

Ptp_callback_environ PCBE

The cleanup group is associated with the thread pool through this struct (note that the internal ptp_pool field in this structure points to the thread pool we created)

Ptp_cleanup_group PTPCG

Point to the cleanup group created by Createthreadpoolcleanupgroup.

ptp_cleanup_group_cancel_callback pfng

cleanupgroupcancelcallback callback function . When the cleanup group Closethreadpoolcleanupgroupmembers function is called and Bcancelpendingcallbacks is passed in true to Clear Cleanup Group , if there are no work items that are not processed at this time, call this callback function with the following prototype:

VOID CALLBACK Cleanupgroupcancelcallback (

PVOID pvobjectcontext,pvoid pvcleanupcontext)

Where Pvobjectcontext is the pvcontext passed through the createthreadpool* function, The parameter pvcleanupcontext is passed in by the Pvcleanupcontext parameter of the closethreadpoolcleanupgroupmembers.

Note:

① whenever a function such as createthreadpool* (such as Createthreadpooltimer) is called, if its last callback environment parameter is not NULL, the created item is added to the cleanup group in the callback environment. Indicates that a new item has been added to the thread pool and requires a potential cleanup .

② If you call closethreadpool*(such as Closethreadpooltimer), you are implicitly deleting the corresponding item from the cleanup group .

(3) Release cleanup Group member: Closethreadpoolcleanupgroupmembers (the closethreadpool* function is called for each work item of the cleanup team fairly)

Parameters

Describe

Ptp_cleanup_group PTPCG

Point to the cleanup group created by Createthreadpoolcleanupgroup.

BOOL Bcancelpendingcallbacks

Whether to cancel the thread pool is the remaining work item.

① if bcancelpendingcallbacks is specified as True, all submitted but not processed work items will be canceled directly. If the callback function was previously specified in the Setthreadpoolcallbackcleanupgroup pfng, the callback function pfng is called once for each item that was canceled .

② if bcancelpendingcallbacks is False, the thread pool takes time to process the remaining work items in the queue before the function returns. (The Cleanupgroupcancelcallback function is not called, so pvcleanupcontext can be set to null)

PVOID Pvcleanupcontext

Additional parameters passed to the Cleanupgroupcancelcallback callback function. If Bcancelpendingcallbacks is false, Cleanupgroupcancelcallback is not called, so the parameter can be set to null.

Note: When the function is called by the master thread, the function waits until all remaining work items in the thread pool have been processed before returning. (This is similar to the waitforthreadpool* function (such as waitforthreadpoolwork).

(4) Destroy cleanup Group: Closethreadpoolcleanupgroup will clean up the memory of the group itself!

11th Chapter Windows thread Pool (3) _ Private thread pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.