日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ATL 线程池的使用

發布時間:2025/3/15 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ATL 线程池的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一.自定義一個Worker

class CMyWorker{public:typedef MyRequestType RequestType;BOOL Initialize(void* pvWorkerParam);void Execute(MyRequestType request, void* pvWorkerParam, OVERLAPPED* pOverlapped);void Terminate(void* pvWorkerParam);};

必須實現以上接口

具體參考如下:

http://msdn.microsoft.com/zh-cn/library/ytkt93h8.aspx

Demo:

LONG g_lCurrId = -1;class CMyWorker { public:typedef DWORD_PTR RequestType;CMyWorker() : m_dwExecs( 0 ){m_lId = InterlockedIncrement( &g_lCurrId );}virtual BOOL Initialize(void *pvParam){printf("[%d]: CMyWorker.Initialize(%d)\n", (DWORD_PTR)::GetCurrentThreadId(), (DWORD_PTR)pvParam );return TRUE;}virtual void Terminate(void* /*pvParam*/){printf( "CMyWorker #%d exec'd %d times.\n", m_lId, m_dwExecs );}void Execute(RequestType dw, void *pvParam, OVERLAPPED* pOverlapped) throw(){ATLASSERT(pvParam != NULL);printf("[%d] CMyWorker::Execute(dw=%d, pvParam=%d, pOverlapped=%d\n", ::GetCurrentThreadId(), dw, (DWORD_PTR)pvParam, (DWORD_PTR)pOverlapped);CTaskBase* pTask = (CTaskBase*)(DWORD_PTR)dw;pTask->DoTask(pvParam, pOverlapped);m_dwExecs++;}virtual BOOL GetWorkerData(DWORD /*dwParam*/, void ** /*ppvData*/){return FALSE;}protected:DWORD m_dwExecs;LONG m_lId; }; // CMyWorker

二.線程池(CThreadPool)

#define THREADPOOL_SIZE 5/// int main(int /*argc*/, char* /*argv[]*/) {CThreadPool<CMyWorker> pool;CTaskArray tasks;HRESULT hr = pool.Initialize((void*)321, THREADPOOL_SIZE);if( SUCCEEDED( hr ) ) {int i = -1;CTaskBase* pTask = NULL;if ( CreateTasks(tasks, 100) ) {for ( i = 0; i < tasks.GetSize(); i++ ) {pTask = tasks[ i ];ATLASSERT( NULL != pTask );pool.QueueRequest( (CMyWorker::RequestType) pTask );}}// Allow a little time for all the tasks to completeSleep(1000);// Clean up the tasks and shutdown the thread poolfor ( i = 0; i < tasks.GetSize(); i++ ) {pTask = tasks[ i ];ATLASSERT( NULL != pTask );delete pTask;}// Shutdown the thread poolpool.Shutdown();}elseprintf("Failed to init thread pool!");printf("\n");return 0; }

參考:http://msdn.microsoft.com/zh-cn/library/dta23y51.aspx

  • Initialize初始化線程數量,并初始化Worker
  • QueueRequest則調用Worker的Execute方法
  • 等Worker全部執行完畢以后才調用Terminate方法
  • 三.IThreadPoolConfig

    定義了一個基礎的接口

    // Used to configure the worker thread pool. This can be used by any // client of the CThreadPool class. __interface __declspec(uuid("B1F64757-6E88-4fa2-8886-7848B0D7E660"))IThreadPoolConfig : public IUnknown {STDMETHOD(SetSize)(_In_ int nNumThreads);STDMETHOD(GetSize)(_Out_ int *pnNumThreads);STDMETHOD(SetTimeout)(_In_ DWORD dwMaxWait);STDMETHOD(GetTimeout)(_Out_ DWORD *pdwMaxWait); };

    四.ThreadTraits

    分兩種CRTThreadTraits和Win32ThreadTraits,分別調用_beginthreadex和CreateThread方法

    五.WaitTraits

    只實現了Win32WaitTraits了,即調用了WaitForSingleObject

    轉載于:https://www.cnblogs.com/Clingingboy/archive/2011/07/16/2108391.html

    總結

    以上是生活随笔為你收集整理的ATL 线程池的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。