Hi,all:
When I was trying to design a thread pool, after thread job done, I need to notify the pool to do some work,like moving this thread from busy list to idle list,I designed 3 classes in 3 .h files:
1. CThreadPoolBase,an interface for an ending thread to notify the concrete thread pool to do some work:
clase CThreadPoolBase
{
public:
virtual ~CThreadPoolBase()=0{}
virtual void OnThreadFinished(LPVOID thr)=0;
}
2. CThreadPool: the concrete thread pool that fix the job:
#include "CThreadPoolBase.h"
class CThreadPool:public CThreadPoolBase
{
public:
...
void OnThreadFinished(LPVOID thr)
{
<pre lang="cs">CThread *pthr = static_cast<CThread*>(thr);
if( pthr != NULL )
MoveToIdleList(pthr);
else
{
cout<<"\t***[ERR_OnThreadFinished], Received error thread notify."<<endl;
return;
}
int noff = GetIdleNum()-GetInitNum();
if (noff>0)
{
DeleteIdleThread(noff);
}
}
}
3. Thread: class of thread handling:
#include "CThreadPoolBase.h
class Thread:
{
public:
Thread(CThreadPoolBase *pool=NULL):m_pool(pool){}
~Thread(){}
...
BOOL Thread::OnTask()
{
...// do some work here
//notify the pool that 'this' thread has finished its work
m_pool->OnThreadFinished(this); //Exception caught <big>sometimes</big>
return TRUE;
}
private:
CThreadPoolBase *m_pool;
}
I got this exception:
Unhandled exception in .exe 0xC0000005 access violation
25: m_pool->OnThreadFinished(this);
0040205C mov esi,esp
0040205E mov ecx,dword ptr [ebp-4]
00402061 push ecx
00402062 mov edx,dword ptr [ebp-4]
00402065 mov ecx,dword ptr [edx+50h]
00402068 mov eax,dword ptr [ebp-4]
0040206B mov edx,dword ptr [eax+50h]
0040206E mov eax,dword ptr [edx]
//it stopped here
00402070 call dword ptr [eax+4]
00402073 cmp esi,esp
00402075 call __chkesp (0040ed50)
Anybody would kindly tell me, what's the problem, and how did it happen?
Thank you !