/////////////////////////////////////////////////////////////////////////////// // // main.cpp // // Feb '00 - Microsoft Systems Journal - Bugslayer Column - John Robbins // /////////////////////////////////////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #include #include #include #include "xtib.h" /////////////////////////////////////////////////////////////////////////////// // ThreadProc DWORD WINAPI ThreadProc(LPVOID lpParameter) { HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, _T("ThreadNameEvent")); if (NULL == hEvent) { OutputDebugString(_T("Unable to open the event!\n")); return 0; } // The event is active so set the thread name. Since the threads // routine's local variables are guarenteed to be around for the // life of the thread, I can set the name using this string // buffer. TCHAR szThreadName[MAX_PATH]; wsprintf(szThreadName, _T("Hello from thread %d!"), lpParameter); if (FALSE == XTIB::SetThreadName(szThreadName)) { OutputDebugString(_T("Unable to set the XTIB for :\n")); OutputDebugString(szThreadName); return 0; } DWORD tid = GetCurrentThreadId(); tid; WaitForSingleObject(hEvent, INFINITE); Sleep(2000); CloseHandle(hEvent); return 1; } /////////////////////////////////////////////////////////////////////////////// // main void main(void) { // Set the name for the main thread if (FALSE == XTIB::SetThreadName(_T("Hello from the main thread!"))) { OutputDebugString(_T("Unable to set main thread name!\n")); return; } DWORD tid = GetCurrentThreadId(); HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, _T("ThreadNameEvent")); if (NULL == hEvent) { OutputDebugString(_T("Unable to create the event!\n")); } // Create five threads for (DWORD i = 1; i < 6; i++) { HANDLE hThread = CreateThread(NULL, 0, ThreadProc, (LPVOID)i, 0, &tid); if (NULL == hThread) { SetEvent(hEvent); CloseHandle(hEvent); OutputDebugString(_T("Unable to create the thread!\n")); return; } } Sleep(5000); SetEvent(hEvent); Sleep(1000); CloseHandle(hEvent); }