QueueUserWorkItem 初步学习

QueueUserWorkItem 初步学习 DWORDWINAPITestThreadPool(PVOIDpContext);CRITICAL_SECTIONg_cs;int_tmain(intargc,_TCHAR*argv[]){ InitializeCriticalSection(&g_cs); SECURITY_ATTRIBUTESsa; sa.bInheritHandle=TRUE; sa.nLen

大家好,欢迎来到IT知识分享网。QueueUserWorkItem 初步学习

 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 < 10; 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(1000);
 
 if (wcscmp(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;
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/12789.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信