C++线程池源代码实现2


FORCEINLINE
VOID
InitializeListHead(
       IN PLIST_ENTRY ListHead
       )
{
 ListHead->Flink = ListHead->Blink = ListHead;
}

FORCEINLINE BOOLEAN RemoveEntryList(IN PLIST_ENTRY Entry)
{
 PLIST_ENTRY Blink;
 PLIST_ENTRY Flink;

 Flink = Entry->Flink;
 Blink = Entry->Blink;
 Blink->Flink = Flink;
 Flink->Blink = Blink;
 return (BOOLEAN)(Flink == Blink);
}

FORCEINLINE VOID InsertHeadList(IN PLIST_ENTRY ListHead,IN PLIST_ENTRY Entry)
{
 PLIST_ENTRY Flink;

 Flink = ListHead->Flink;
 Entry->Flink = Flink;
 Entry->Blink = ListHead;
 Flink->Blink = Entry;
 ListHead->Flink = Entry;
}

FORCEINLINE VOID InsertTailList(
      IN PLIST_ENTRY ListHead,
      IN PLIST_ENTRY Entry
      )
{
 PLIST_ENTRY Blink;

 Blink = ListHead->Blink;
 Entry->Flink = ListHead;
 Entry->Blink = Blink;
 Blink->Flink = Entry;
 ListHead->Blink = Entry;
}


#define InitializeListHead(ListHead) (/
 (ListHead)->Flink = (ListHead)->Blink = (ListHead))

#define RemoveEntryList(Entry) {/
 PLIST_ENTRY _EX_Blink;/
 PLIST_ENTRY _EX_Flink;/
 _EX_Flink = (Entry)->Flink;/
 _EX_Blink = (Entry)->Blink;/
 _EX_Blink->Flink = _EX_Flink;/
 _EX_Flink->Blink = _EX_Blink;/
}

#define InsertTailList(ListHead,Entry) {/
 PLIST_ENTRY _EX_Blink;/
 PLIST_ENTRY _EX_ListHead;/
 _EX_ListHead = (ListHead);/
 _EX_Blink = _EX_ListHead->Blink;/
 (Entry)->Flink = _EX_ListHead;/
 (Entry)->Blink = _EX_Blink;/
 _EX_Blink->Flink = (Entry);/
 _EX_ListHead->Blink = (Entry);/
}

#define RemoveHeadList(ListHead) /
 (ListHead)->Flink;/
{RemoveEntryList((ListHead)->Flink)}


#define IsListEmpty(ListHead) /
 ((ListHead)->Flink == (ListHead))


typedef struct _THREAD_POOL
{
  HANDLE QuitEvent;
  HANDLE WorkItemSemaphore;

  LONG WorkItemCount;
  LIST_ENTRY WorkItemHeader;
  CRITICAL_SECTION WorkItemLock;

  LONG ThreadNum;
  HANDLE *ThreadsArray;

}THREAD_POOL, *PTHREAD_POOL;

typedef VOID (*WORK_ITEM_PROC)(PVOID Param);

typedef struct _WORK_ITEM
{
  LIST_ENTRY List;

  WORK_ITEM_PROC UserProc;
  PVOID UserParam;
 
}WORK_ITEM, *PWORK_ITEM;


DWORD WINAPI WorkerThread(PVOID pParam)
{
  PTHREAD_POOL pThreadPool = (PTHREAD_POOL)pParam;
  HANDLE Events[2];
 
  Events[0] = pThreadPool->QuitEvent;
  Events[1] = pThreadPool->WorkItemSemaphore;

  for(;;)
  {
      DWORD dwRet = WaitForMultipleObjects(2, Events, FALSE, INFINITE);

      if(dwRet == WAIT_OBJECT_0)
          break;

      else if(dwRet == WAIT_OBJECT_0 +1)
      {
          PWORK_ITEM pWorkItem;
          PLIST_ENTRY pList;

          EnterCriticalSection(&pThreadPool->WorkItemLock);
          _ASSERT(!IsListEmpty(&pThreadPool->WorkItemHeader));
          pList = RemoveHeadList(&pThreadPool->WorkItemHeader);
          LeaveCriticalSection(&pThreadPool->WorkItemLock);

          pWorkItem = CONTAINING_RECORD(pList, WORK_ITEM, List);
          pWorkItem->UserProc(pWorkItem->UserParam);

          InterlockedDecrement(&pThreadPool->WorkItemCount);
          free(pWorkItem);
      }

      else
      {
          _ASSERT(0);
          break;
      }
  }

  return 0;
}

BOOL InitializeThreadPool(PTHREAD_POOL pThreadPool, LONG ThreadNum)
{
  pThreadPool->QuitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  pThreadPool->WorkItemSemaphore = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
  pThreadPool->WorkItemCount = 0;
  InitializeListHead(&pThreadPool->WorkItemHeader);
  InitializeCriticalSection(&pThreadPool->WorkItemLock);
  pThreadPool->ThreadNum = ThreadNum;
  pThreadPool->ThreadsArray = (HANDLE*)malloc(sizeof(HANDLE) * ThreadNum);

  for(int i=0; i<ThreadNum; i++)
  {
      pThreadPool->ThreadsArray[i] = CreateThread(NULL, 0, WorkerThread, pThreadPool, 0, NULL);
  }

  return TRUE;
}

VOID DestroyThreadPool(PTHREAD_POOL pThreadPool)
{
  SetEvent(pThreadPool->QuitEvent);

  for(int i=0; i<pThreadPool->ThreadNum; i++)
  {
      WaitForSingleObject(pThreadPool->ThreadsArray[i], INFINITE);
      CloseHandle(pThreadPool->ThreadsArray[i]);
  }

  free(pThreadPool->ThreadsArray);

  CloseHandle(pThreadPool->QuitEvent);
  CloseHandle(pThreadPool->WorkItemSemaphore);
  DeleteCriticalSection(&pThreadPool->WorkItemLock);

  while(!IsListEmpty(&pThreadPool->WorkItemHeader))
  {
      PWORK_ITEM pWorkItem;
      PLIST_ENTRY pList;
     
      pList = RemoveHeadList(&pThreadPool->WorkItemHeader);
      pWorkItem = CONTAINING_RECORD(pList, WORK_ITEM, List);
     
      free(pWorkItem);
  }
}

BOOL PostWorkItem(PTHREAD_POOL pThreadPool, WORK_ITEM_PROC UserProc, PVOID UserParam)
{
  PWORK_ITEM pWorkItem = (PWORK_ITEM)malloc(sizeof(WORK_ITEM));

  if(pWorkItem == NULL)
      return FALSE;

  pWorkItem->UserProc = UserProc;
  pWorkItem->UserParam = UserParam;

  EnterCriticalSection(&pThreadPool->WorkItemLock);
  InsertTailList(&pThreadPool->WorkItemHeader, &pWorkItem->List);
  LeaveCriticalSection(&pThreadPool->WorkItemLock);

  InterlockedIncrement(&pThreadPool->WorkItemCount);
  ReleaseSemaphore(pThreadPool->WorkItemSemaphore, 1, NULL);

  return TRUE;
}

VOID UserProc1(PVOID dwParam)
{
  // WorkItem(dwParam);
 printf("%d/n",rand());
}

void TestThreadPool(BOOL bWaitMode, LONG ThreadNum)
{
  THREAD_POOL ThreadPool;   
  InitializeThreadPool(&ThreadPool, ThreadNum);
 
  CompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  DWORD dwStart = GetTickCount();
  int ItemCount = 20;

  for(int i=0; i<20; i++)
  {
      PostWorkItem(&ThreadPool, UserProc1, (PVOID)bWaitMode);
  }
 
  WaitForSingleObject(CompleteEvent, INFINITE);
  CloseHandle(CompleteEvent);

  DestroyThreadPool(&ThreadPool);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值