I'm currently learning about the Threadpool API as part of the WinAPI. I successfully created a threadpool and used SubmitThreadpoolWork to execute worker threads. However, so far I'm only able to pass one static value to all the worker threads (specified as the 2. parameter of the CreateThreadpoolWork function). How can I pass different values to each worker thread?
My callback function:
void CALLBACK test(PTP_CALLBACK_INSTANCE Instance, PVOID Parameter, PTP_WORK Work) {
Sleep(2000);
int* val = (int*)Parameter;
std::wcout << (*val) << std::endl;
}
My main function:
int main() {
TP_CALLBACK_ENVIRON callBackEnviron;
PTP_POOL pool = NULL;
PTP_CLEANUP_GROUP cleanupgroup = NULL;
PTP_WORK_CALLBACK workcallback = test;
PTP_TIMER timer = NULL;
PTP_WORK work = NULL;
InitializeThreadpoolEnvironment(&callBackEnviron);
pool = CreateThreadpool(NULL);
SetThreadpoolThreadMaximum(pool, 8);
cleanupgroup = CreateThreadpoolCleanupGroup();
SetThreadpoolCallbackPool(&callBackEnviron, pool);
SetThreadpoolCallbackCleanupGroup(&callBackEnviron, cleanupgroup, NULL);
for (int i = 0; i < 20; ++i)
{
work = CreateThreadpoolWork(workcallback, &i, &callBackEnviron);
SubmitThreadpoolWork(work);
}
WaitForThreadpoolWorkCallbacks(work, FALSE);
CloseThreadpoolWork(work);
CloseThreadpoolCleanupGroupMembers(cleanupgroup, FALSE, NULL);
CloseThreadpoolCleanupGroup(cleanupgroup);
CloseThreadpool(pool);
}
Note that every worker thread outputs "20" which is the final value of i before the for-loop is finished. However, I want each worker thread to output the value of i when the respective SubmitThreadpoolWork call is executed. I'm also fairly certain that CreateThreadpoolWork is not supposed to be called in a loop, but I wasn't sure on how to attempt it otherwise.