Wednesday, November 3, 2010

Windows Thread Pool

Tip -We can use Windows thread pool for gaining performance in a multithreaded environment.

Details -In a multithreaded environment, we create several worker threads to perform various tasks in parallel.  Usually we create threads as and when required and exit the thread after finishing the job. In order to relieve the programmer from the thread creation and destruction tasks and to improve the performance, we can use the Windows thread pool. Windows has redesigned its thread pool architecture starting from Vista. Using this, we can execute a normal worker thread task without calling the CreateThread() API, in a thread. Briefly we can use the thread pool APIs to call a user defined function under the following situations.

  • Execute the function in a thread.
  • Execute the function during the timer intervals.
  • Execute the function whenever a kernel object signals.
  • Execute the function when an asynchronous IO is completed.
API -In order to execute a function in a separate thread in the pool, define a function with the following signature.

VOID CALLBACK AsyncFunction( PTP_CALLBACK_INSTANCE pInstance_i, PVOID pvContext_i );

Now call the following API, to execute the above function in a separate thread.

BOOL WINAPI TrySubmitThreadpoolCallback(PTP_SIMPLE_CALLBACK pThreadFun_i, PVOID pvData_i, PTP_CALLBACK_ENVIRON pEnv_i );

The above user define function will be passed as the first parameter to the thread pool API. 

References:

Posted By : Mohammed Nisamudheen S.

No comments:

Post a Comment