创建一个新线程
#include <Windows.h>
DWORD WINAPI DoStuff(LPVOID lpParameter)
{
// The new thread will start here
return 0;
}
int main()
{
// Create a new thread which will start at the DoStuff function
HANDLE hThread = CreateThread(
NULL, // Thread attributes
0, // Stack size (0 = use default)
DoStuff, // Thread start address
NULL, // Parameter to pass to the thread
0, // Creation flags
NULL); // Thread id
if (hThread == NULL)
{
// Thread creation failed.
// More details can be retrieved by calling GetLastError()
return 1;
}
// Wait for thread to finish execution
WaitForSingleObject(hThread, INFINITE);
// Thread handle must be closed when no longer needed
CloseHandle(hThread);
return 0;
}
请注意,CRT 还提供了用于创建线程的 _beginthread
和 _beginthreadex
API,此示例中未显示。以下链接讨论了这些 API 与 CreateThread
API 之间的差异 。
参考文献(MSDN):