?? 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(1000);
// 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 + -