Monday, August 01, 2011

Windows ThreadPool APIs

The ThreadPool APIs are pretty cool, but the docs are confusing at first blush and the sample code is horrible (just one sample, and suffers from kitchen-sink syndrome). But fear not! MSDN lets users comment, so I did :-) Repro'd here for posterity:

The sample is rather big and sprawling, making it hard to understand w/o knowing most of the API surface. To just execute a simple block of work, it's actually pretty simple:

#include <windows.h>
#include <stdio.h>

void NTAPI MyWorkCallback(PTP_CALLBACK_INSTANCE instance, void * context, PTP_WORK work)
{

printf("Hello %s", context);
}

int main()
{

TP_WORK * work = CreateThreadpoolWork(MyWorkCallback, "world", NULL);
if (work == NULL)
printf("Error %d in CreateThreadpoolWork", GetLastError());
else {
SubmitThreadpoolWork(work);
WaitForThreadpoolWorkCallbacks(work, FALSE);
CloseThreadpoolWork(work);
}
}

Posted via email from Jasper-Net