Test1.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <assert.h> #include <windows.h>dword WINAPI Testthreadpool (PVOID pContext); critical_section g_cs;int _tmain (int argc, _tchar* argv[]) {initializecriticalsection (&G_CS); Security_attributes sa;sa.binherithandle = true;sa.nlength = sizeof (security_attributes); sa.lpsecuritydescriptor = NULL; HANDLE Hsemaphore = createsemaphorew (&sa, 0, 1, L "Jyytet"); for (int i = 0; i < i++) {QueueUserWorkItem (Testthreadpool, L "Hello World", Wt_executedefault);} QueueUserWorkItem (Testthreadpool, L "Finish", Wt_executedefault); WaitForSingleObject (Hsemaphore,-1); CloseHandle (hsemaphore); hsemaphore = NULL; DeleteCriticalSection (&g_cs); wprintf (L "Game over/r/n"); GetChar (); return 0;} DWORD WINAPI Testthreadpool (PVOID pContext) {if (NULL = = PContext) return 0; EnterCriticalSection (&g_cs); wchar_t *pprintf = (wchar_t *) pcontext;wprintf (L "%s/r/n", pprintf); Sleep (WCSCMP), if (pprintf, L "FinIsh ") = = 0) {HANDLE Hsemaphore = Opensemaphorew (semaphore_all_access, FALSE, L" Jyytet "); assert (Hsemaphore! = NULL); ReleaseSemaphore (Hsemaphore, 1, NULL); CloseHandle (Hsemaphore);} LeaveCriticalSection (&g_cs); return 1;}
Thread pool QueueUserWorkItem