#include "stdafx.h"
#include "ThreadBase.h"
#include <process.h>

CThreadBase::CThreadBase()
: m_bExit(FALSE)
, m_hThread(NULL)
{
	
}

CThreadBase::~CThreadBase()
{
	
}

BOOL CThreadBase::start()
{
	m_hThread = (HANDLE)_beginthreadex(0, 0, HeartBeat, this, 0, 0);
	//CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)HeartBeat, this, 0, NULL);
	return TRUE;
}

BOOL CThreadBase::end()
{
	m_bExit = TRUE;
	WaitForSingleObject(m_hThread, INFINITE);
	CloseHandle(m_hThread);
	return TRUE;
}

unsigned int _stdcall CThreadBase::HeartBeat(void *pParam)
{
	CThreadBase *pThread = (CThreadBase *)pParam;
	while (!pThread->m_bExit)
	{
		pThread->HeartBeat_Imp();
		Sleep(10);
	}
	return 1;
}

void CThreadBase::pushMsg( CInterProtocl *pdu)
{
	m_queMutex.acquire();
	m_pduQueue.push_back(pdu);
	m_queMutex.release();
}