Programming Applications for Microsoft Windows (Microsoft Programming Series)

[Previous] [Next]

Every thread must have an entry-point function where it begins execution. We already discussed this entry-point function for your primary thread: main, wmain, WinMain, or wWinMain. If you want to create a secondary thread in your process, it must also have an entry-point function, which should look something like this:

DWORD WINAPI ThreadFunc(PVOID pvParam){ DWORD dwResult = 0;. return(dwResult); }

Your thread function can perform any task you want it to. Ultimately, your thread function will come to an end and return. At this point, your thread stops running, the memory for its stack is freed, and the usage count of your thread's kernel object is decremented. If the usage count becomes 0, the thread kernel object is destroyed. Like process kernel objects, thread kernel objects always live at least as long as the thread they are associated with, but the object might live well beyond the lifetime of the thread itself.

Let me point out a few things about thread functions:

Now that you know how to implement a thread function, let's talk about how to actually get the operating system to create a thread that executes your thread function.

Категории