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); } }

That dispatches 1 work item to the default threadpool and blocks until it finishes.

And that has full error handling. It really is that simple.

This article is quite helpful to understand the API =>

Technorati : Win32 Thread : Win32 Thread

Zooomr : Win32 Thread

Flickr : Win32 Thread