📄 mthello.cpp
字号:
#include <windows.h>
#include <stdio.h>
// Prototype for Hello application worker thread procedure.
//
DWORD WINAPI WorkerThreadProc( LPVOID lpThreadParameter );
// Main program entry point.
//
int main( void )
{
HANDLE hWorkerThread;
DWORD dwWorkerThreadId;
DWORD dwPrimaryThreadId;
// Determine and save away the primary thread's identifier.
//
dwPrimaryThreadId = GetCurrentThreadId();
printf(
"[%08x] Multithreaded hello application started.\n",
dwPrimaryThreadId
);
// Launch the worker thread.
//
hWorkerThread =
CreateThread(
NULL, // Default security attributes.
0, // Default stack size.
WorkerThreadProc, // Thread proc to start at.
(LPVOID)3, // Argument: work for 3 seconds.
0, // No special flags.
&dwWorkerThreadId // Out: thread id of worker.
);
if( hWorkerThread != NULL )
{
printf(
"[%08x] Worker thread ID = 0x%08x.\n",
dwPrimaryThreadId,
dwWorkerThreadId
);
// Give the worker thread time to do its thing.
//
Sleep(6000);
// Read the worker's thread exit code and cleanup.
//
DWORD dwThreadExitCode;
GetExitCodeThread(hWorkerThread, &dwThreadExitCode);
printf(
"[%08x] Worker thread exit code = 0x%08x.\n",
dwPrimaryThreadId,
dwThreadExitCode
);
CloseHandle(hWorkerThread);
}
else
{
printf(
"[%08x] Failed to create worker thread: error 0x%x\n",
dwPrimaryThreadId,
GetLastError()
);
}
printf(
"[%08x] Multithreaded hello application exiting.\n",
dwPrimaryThreadId
);
return(0);
}
// Thread entry procedure for the worker thread.
//
DWORD WINAPI WorkerThreadProc( LPVOID lpThreadParameter )
{
// For this thread, lpThreadParameter is used to indicate
// the number of seconds this thread should execute for.
//
DWORD dwNumSeconds = (DWORD)lpThreadParameter;
DWORD dwThreadId = GetCurrentThreadId();
while( dwNumSeconds-- > 0 )
{
Sleep(1000);
printf(
"[%08x] Hello, multithreaded world!\n",
dwThreadId
);
}
// Return from thread procedure and specify an exit code
// equal to our thread id.
//
return(dwThreadId);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -