【設計模式】單例模式之執行緒類
阿新 • • 發佈:2019-01-28
好記性,不如爛筆頭。對於單例模式的理解和應用還是需要多多實踐,這次有感而發,寫份基於執行緒執行的單例模式。
單例模式該怎樣去實現:建構函式宣告為private或protect防止被外部函式例項化,內部儲存一個private static的類指標儲存唯一的例項,例項的動作由一個public的類方法代勞,該方法也返回單例類唯一的例項。
標頭檔案:
#pragma once
#include <Windows.h>
#include <vector>
class CMyThread
{
public:
CMyThread(){};
virtual ~CMyThread(){};
virtual void DoThread()
{
printf("****\n");
};
};
class CThreadFunNode
{
public:
CMyThread* m_pThread;
CThreadFunNode()
{
m_pThread = NULL;
};
~CThreadFunNode(){};
};
class CThreadFunList
{
public:
CThreadFunList(){};
~CThreadFunList()
{
std ::vector<CThreadFunNode*>::iterator it = m_FunList.begin();
for (; it != m_FunList.end(); ++it)
{
CThreadFunNode* pNode = (*it);
delete pNode;
pNode = NULL;
}
m_FunList.clear();
};
void AddFun(CMyThread* v_pThread)
{
CThreadFunNode* pNode = new CThreadFunNode;
if (pNode)
{
pNode->m_pThread = v_pThread;
m_FunList.push_back(pNode);
}
};
void Excute()
{
std::vector<CThreadFunNode*>::iterator it = m_FunList.begin();
for (; it != m_FunList.end(); ++it)
{
CThreadFunNode* pNode = (*it);
pNode->m_pThread->DoThread();
}
}
private:
std::vector<CThreadFunNode*> m_FunList;
};
class CThreadObject
{
public:
~CThreadObject();
void Start();
void Stop();
void AddFun(CMyThread* v_pThread);
void ThreadProc();
static CThreadObject* GetInstance();
static void Release();
private:
CThreadObject();
static CThreadObject* m_pThreadObject;
CThreadFunList m_FunList;
HANDLE m_hThread;
BOOL m_bThreadCanRun;
};
原始檔:
#include "stdafx.h"
#include "MyThread.h"
DWORD WINAPI GlobalProc(LPVOID v_lpParam)
{
CThreadObject* pObject = (CThreadObject*)v_lpParam;
pObject->ThreadProc();
return 0;
}
CThreadObject* CThreadObject::m_pThreadObject = NULL;
CThreadObject::CThreadObject()
{
m_bThreadCanRun = FALSE;
DWORD dwThread = 0;
m_hThread = CreateThread(NULL, 0, GlobalProc, this, 0, &dwThread);
}
CThreadObject::~CThreadObject()
{
m_bThreadCanRun = FALSE;
if (m_hThread)
{
if (WAIT_OBJECT_0 != WaitForSingleObject(m_hThread, 100L))
{
TerminateThread(m_hThread, 0);
}
CloseHandle(m_hThread);
}
}
void CThreadObject::AddFun( CMyThread* v_pThread )
{
m_FunList.AddFun(v_pThread);
}
void CThreadObject::ThreadProc()
{
while(TRUE)
{
if (m_bThreadCanRun)
{
m_FunList.Excute();
}
}
}
void CThreadObject::Start()
{
m_bThreadCanRun = TRUE;
}
void CThreadObject::Stop()
{
m_bThreadCanRun = FALSE;
}
CThreadObject* CThreadObject::GetInstance()
{
if (NULL == m_pThreadObject)
{
m_pThreadObject = new CThreadObject;
}
return m_pThreadObject;
}
void CThreadObject::Release()
{
if(NULL != m_pThreadObject)
{
delete m_pThreadObject;
m_pThreadObject = NULL;
}
}
應用方法:
#include "stdafx.h"
#include "MyThread.h"
class CTestThread : public CMyThread
{
public:
CTestThread(){};
~CTestThread(){};
void DoThread()
{
printf("###\n");
};
};
int _tmain(int argc, _TCHAR* argv[])
{
CTestThread TestThread;
CThreadObject* pThreadObject = CThreadObject::GetInstance();
pThreadObject->AddFun(&TestThread);
pThreadObject->Start();
Sleep(100);
pThreadObject->Stop();
pThreadObject->Release();
system("pause");
return 0;
}