Home > Back-end >  Questions about appropriate destruction of the thread pool
Questions about appropriate destruction of the thread pool

Time:09-28

# include "stdafx. H"
# include & lt; Windows. H>

Void WINAPI FunCallBackForWork (PTP_CALLBACK_INSTANCE pInstance, PVOID Context, PTP_WORK pwork)
{
Printf (" % d \ n ", * (int *) Context).
return;
}

Void WINAPI FunCleanGroupCancelCallback (PVOID pvObjectContext, PVOID pvCleanupContext)
{
Printf (" % s ", "SZN \ n");
}

Int _tmain (int arg c, _TCHAR * argv [])
{
//create a thread pool, CreateThreadPool () function of the first parameter is reserved and must pass in NULL
PTP_POOL pool=CreateThreadpool (NULL);

//set the thread pool threads allowed to allow the maximum and minimum values
SetThreadpoolThreadMaximum (pool, 3);
SetThreadpoolThreadMinimum (pool, 1);

//define a callback environment
TP_CALLBACK_ENVIRON pcbe;
//initialization callback environment
InitializeThreadpoolEnvironment (& amp; Pcbe);
//set the thread pool associated with the callback environment object
SetThreadpoolCallbackPool (& amp; Pcbe, pool);

The int Value=https://bbs.csdn.net/topics/10;
//create a work item display control
PTP_WORK pWork=CreateThreadpoolWork (FunCallBackForWork, & amp; Value, & amp; Pcbe);

//submit three work item
SubmitThreadpoolWork (pWork);
SubmitThreadpoolWork (pWork);
SubmitThreadpoolWork (pWork);

//the following code for the destruction of the thread pool
PTP_CLEANUP_GROUP CleanGroup=CreateThreadpoolCleanupGroup ();
SetThreadpoolCallbackCleanupGroup (& amp; Pcbe CleanGroup, FunCleanGroupCancelCallback);
CloseThreadpoolCleanupGroupMembers (CleanGroup, TRUE, NULL);
CloseThreadpoolCleanupGroup (CleanGroup);
DestroyThreadpoolEnvironment (& amp; Pcbe);
CloseThreadpool (pool);
CloseThreadpoolWork (pWork);
return 0;
}
The program is running the results are as follows:
FunCallBackForWork () is not called, FunCleanGroupCancelCallback () function didn't also called
Doubt point: since FunCallBackForWork () is not call processing has not yet been submitted work items, and then at the time of closing working group CloseThreadpoolCleanupGroupMembers () function is introduced to TRUE, and SetThreadpoolCallbackCleanupGroup () function callback function is not NULL, of every work item has been canceled the callback function will be called, but the application result is the callback function FunCleanGroupCancelCallback () is not called, is this why?
  • Related