VC執行緒池

2021-06-06 19:08:53 字數 3805 閱讀 6516

類定義如下

// threadpoolimp.h: inte***ce for the threadpoolimp class.

////

#if !defined(afx_threadpoolimp_h__82f4fc7e_2db4_4d2a_acc8_2efc787cae42__included_)

#define afx_threadpoolimp_h__82f4fc7e_2db4_4d2a_acc8_2efc787cae42__included_

#if _msc_ver > 1000

#pragma once

#endif // _msc_ver > 1000

#pragma warning( disable : 4705 4786)

#include

#include "autolock.h"

using namespace std;

class ijobdesc;

class iworker;

class cthreadpoolimp

threadinfo(handle handle, bool bbusy)

threadinfo(const threadinfo& info)

handle m_hthread;

bool m_bbusyworking;

};typedef mapthreadinfomap;

typedef threadinfomap::iterator iterator_threadinfomap;

friend static unsigned int cthreadpoolimp::managerproc(void* p);

friend static unsigned int cthreadpoolimp::workerproc(void* p);

protected:

enum threadpoolstatus ;

public:

//inte***ce to the outside

void start(unsigned short nstatic, unsigned short nmax);

void stop(bool bhash=false);

void processjob(ijobdesc* pjob, iworker* pworker) const;

//constructor and destructor

cthreadpoolimp();

virtual ~cthreadpoolimp();

protected:

//inte***ces public:

handle getmgrioport() const

uint getmgrwaittime() const

handle getworkerioport() const

private:

static dword winapi managerproc(void* p);

static dword winapi workerproc(void* p);

protected:

//manager thread

handle m_hmgrthread;

handle m_hmgrioport;

protected:

//configuration parameters

mutable unsigned short m_nnumberofstaticthreads;

mutable unsigned short m_nnumberoftotalthreads;

protected:

//helper functions

void addthreads();

void removethreads();

threadpoolstatus getthreadpoolstatus();

void changestatus(dword threadid, bool status);

void removethread(dword threadid);

protected:

//all the work threads

threadinfomap m_threadmap;

ccriticalsection m_arraycs;

handle m_hworkerioport;

};#endif // !defined(afx_threadpoolimp_h__82f4fc7e_2db4_4d2a_acc8_2efc787cae42__included_)

實現如下

// threadpool.cpp: implementation of the cthreadpoolimp class.

////

#include "stdafx.h"

#include "threadpoolimp.h"

#include "outdebug.h"

#include

#include "work.h"

#ifdef _debug

#undef this_file

static char this_file=__file__;

//#define new debug_new

#endif

cthreadpoolimp::cthreadpoolimp()

cthreadpoolimp::~cthreadpoolimp()

}void cthreadpoolimp::stop(bool bhash)

dword rc=waitformultipleobjects(ncount, pthread, true, 30000);

closehandle(m_hworkerioport);

if(rc>=wait_object_0 && rc

}else if(rc==wait_timeout&&bhash)

closehandle(pthread[i]);}}

delete pthread;

}dword winapi cthreadpoolimp::managerproc(void* p)

else

}//time out processing

if (::getlasterror()==wait_timeout)

return 0;

}dword winapi cthreadpoolimp::workerproc(void* p)

else

}return 0;

}void cthreadpoolimp::changestatus(dword threadid, bool status)

void cthreadpoolimp::processjob(ijobdesc* pjob, iworker* pworker) const

void cthreadpoolimp::addthreads()

}void cthreadpoolimp::removethread(dword threadid)

void cthreadpoolimp::removethreads()

}cthreadpoolimp::threadpoolstatus cthreadpoolimp::getthreadpoolstatus()

if ( ncount/(1.0*ntotal) > 0.8 )

return busy;

if ( ncount/ (1.0*ntotal) < 0.2 )

return idle;

return normal;

}

VC 執行緒池demo 附原始碼)

第18章 vc 執行緒池demo 附原始碼 又不影響主線程工作時!所以我們要想清楚什麼時候需要用到執行緒池!一般都是用在伺服器socket通訊場景是用得最多的 客戶端連線併發.等 當需要處理的任務較少時,我們可以自己建立執行緒去處理,但在高併發場景下,我們需要處理的任務數量很多,由於建立銷毀執行緒開...

執行緒 執行緒池

執行緒池是一種多執行緒處理形式,處理過程中將任務新增到佇列,然後在建立執行緒後執行,主要實現 建立執行緒和管理執行緒,並且給執行緒分配任務。執行緒池中的執行緒是併發執行的。乙個比較簡單的執行緒池至少應包含執行緒池管理器 工作執行緒 任務列隊 任務介面等部分。其中執行緒池管理器的作用是建立 銷毀並管理...

執行緒 執行緒池

乙個簡單執行緒的建立和銷毀如下 與程序程序相比,執行緒是一種輕量級的工具,但是輕量並不代表沒有,它的建立和關閉依然需要花費時間,如果建立和銷毀的時間還大於執行緒本身完成的工作,那就會得不償失,甚至會造成out of memory。即使沒有,大量的執行緒 也會給gc帶來巨大的壓力。為了解決這樣的問題,...