非主流歌曲:Windows核心编程(第五版)笔记 第十一章 线程池(The Windows Threa...

来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 23:32:04
Windows核心编程(第五版)笔记 第十一章 线程池(The Windows Thread Pool)

第十一章 线程池

 

线程池的目的就是为了减少创建和销毁线程的额外开销,利用已经存在的线程多次循环执行多个任务从而提高系统的处理能力.

线程池会自动地根据内制的算法增加或减少线程池中的线程或为程序增加新的线程池。

1.异步方法调用

               

                异步方法调用有以下两种方法:

               

                (1)线程函数原型(回调函数)

                                VOID NTAPI SimpleCallback

                                (

                                   PTP_CALLBACK_INSTANCE pInstance, // See "Callback Termination Actions" section

                                   PVOID pvContext

                                );

 

                                TrySubmitThreadpoolCallback 

                                该函数将线程函数执行请求发到线程池,并将一个"工作项目"添加到线程池的队列中。

                                注:我们不需要调用CreateThread函数,线程池中的线程会执行我们的回调函数

                               

                (2)显示控制"工作项目"

                                CreateThreadpoolWork           创建一个工作项目

                                SubmitThreadpoolWork          将工作项目提交到线程池中,一个工作项目可以多次提交到线程池中。

                                WaitForThreadpoolWorkCallbacks         等待线程函数执行完毕或取消执行线程函数。

                               

                                CreateThreadpoolWork要求的线程函数原型:

                                VOID CALLBACK WorkCallback

                                (

                                                PTP_CALLBACK_INSTANCE Instance,

                                                PVOID Context,

                                                PTP_WORK Work

                                );

 

                                VOID WaitForThreadpoolWorkCallbacks

                                (

                                                PTP_WORK pWork,

                                                BOOL     bCancelPendingCallbacks

                                );

 

2.时间间隔内调用函数

                                (1)CreateThreadpoolTimer要求的线程函数原型

                                VOID CALLBACK TimeoutCallback

                                (

                                                PTP_CALLBACK_INSTANCE pInstance,   // See "Callback Termination Actions" section

                                                PVOID pvContext,

                                                PTP_TIMER pTimer

                                );

                                (2)步骤

                                                CreateThreadpoolTimer

                                                SetThreadpoolTimer

                                                WaitForThreadpoolTimerCallbacks

                                                CloseThreadpoolTimer

                                               

3.当内核对象处于Signal状态时调用函数

                                当指定的内核对象变成Signal状态或等待超时,线程池会用户指定的线程函数。

                                之后当内核对象再次变成Signal状态时,线程函数不会被调用,除非再次调用SetThreadpoolWait注册线程函数。

                               

                                (1)CreateThreadpoolWait要求的线程函数原型

                                VOID CALLBACK WaitCallback

                                (

                                                PTP_CALLBACK_INSTANCE pInstance, // See "Callback Termination Actions" section

                                                PVOID Context,

                                                PTP_WAIT Wait,

                                                TP_WAIT_RESULT WaitResult

                                );

 

                                CreateThreadpoolWait

                                SetThreadpoolWait                不允许多次注册同样的Handle,但是我们可以用DuplicateHandle函数复制一个句柄 然后再注册。

                                WaitForThreadpoolWaitCallbacks

                                CloseThreadpoolWait

 

4.当异步I/O请求结束后调用函数

                                To be filled

 

5.回调终结后的操作和私有线程

                                To be filled

 

6. Common API

                                TrySubmitThreadpoolCallback 

                                CreateThreadpoolWork  SubmitThreadpoolWork  WaitForThreadpoolWorkCallbacks  CloseThreadpoolWork

                                CreateThreadpoolTimer  SetThreadpoolTimer        WaitForThreadpoolTimerCallbacks CloseThreadpoolTimer

                                CreateThreadpoolWait    SetThreadpoolWait          WaitForThreadpoolWaitCallbacks  CloseThreadpoolWait

                                CallbackMayRunLong     DisassociateCurrentThreadFromCallback