线程池QueueUserWorkItem

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

  1. // Test1.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <assert.h>  
  6. #include <Windows.h>  
  7.   
  8. DWORD WINAPI TestThreadPool(PVOID pContext);  
  9.   
  10. CRITICAL_SECTION g_cs;  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     InitializeCriticalSection(&g_cs);  
  15.     SECURITY_ATTRIBUTES sa;  
  16.     sa.bInheritHandle = TRUE;  
  17.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);  
  18.     sa.lpSecurityDescriptor = NULL;  
  19.   
  20.     HANDLE hSemaphore = CreateSemaphoreW(&sa, 0 ,1, L"jyytet");   
  21.   
  22.     for (int i = 0; i < 10; i++)  
  23.     {  
  24.         QueueUserWorkItem(TestThreadPool, L"Hello World", WT_EXECUTEDEFAULT);  
  25.     }  
  26.     QueueUserWorkItem(TestThreadPool, L"Finish", WT_EXECUTEDEFAULT);  
  27.   
  28.     WaitForSingleObject(hSemaphore, -1);  
  29.     CloseHandle(hSemaphore);  
  30.     hSemaphore = NULL;   
  31.     DeleteCriticalSection(&g_cs);  
  32.   
  33.     wprintf(L"Game over /r/n");  
  34.   
  35.     getchar();  
  36.   
  37.     return 0;  
  38. }  
  39.   
  40. DWORD WINAPI TestThreadPool(PVOID pContext)  
  41. {  
  42.     if (NULL == pContext)  
  43.         return 0;  
  44.   
  45.     EnterCriticalSection(&g_cs);  
  46.     wchar_t *pPrintf = (wchar_t *) pContext;  
  47.     wprintf(L"%s /r/n", pPrintf);  
  48.     Sleep(1000);  
  49.   
  50.     if (wcscmp(pPrintf, L"Finish") == 0)  
  51.     {  
  52.         HANDLE hSemaphore = OpenSemaphoreW(SEMAPHORE_ALL_ACCESS, FALSE,  L"jyytet");  
  53.         assert(hSemaphore !=  NULL);  
  54.         ReleaseSemaphore(hSemaphore, 1, NULL);  
  55.         CloseHandle(hSemaphore);  
  56.     }  
  57.     LeaveCriticalSection(&g_cs);  
  58.   
  59.     return 1;  
  60. }  
// 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 < 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;}

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值