Chinaunix首页 | 论坛 | 博客
  • 博客访问: 384563
  • 博文数量: 35
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 797
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-02 08:51
文章分类

全部博文(35)

文章存档

2011年(1)

2010年(3)

2009年(3)

2008年(28)

我的朋友

分类: WINDOWS

2008-05-16 17:24:33

首先,我想应该先了解一下什么是线程,操作系统里说线程是系统可以管理的最小的单元,线程管理里--操作系统控制着他的CPU使用;而我们抛开系统 的概念,线程又是什么呢?线程是一系列可以独立执行的指令的堆栈,我们自己控制着代码的执行,这就是为什么线程函数里都要有一个循环的原因,只不过为了更 好的利用硬件的资源,我们引入了系统的概念,而多线程就是多套相互独立的指令堆栈,如果没有系统的管理线程的代码也是可以单独执行的,系统的最重要的工作 是保护好线程堆栈的现场。

面向对象的方法让我们很好的管理资源,所有设计到系统内核的资源,我们都有可能忘掉还给系统的时候,这时候就会出现系统资源的浪费,自从OO方法的 出现就引入了利用对象来管理资源的方法,微软在MFC已经采用了这样的方法,但微软的方法太臃肿(个人认为i),在.NET平台推出以后更是这样;在 LINIUX上也有很多这样的类库的代码。

下面是我个人写的一个类库,利用这个库可以很好的管理线程资源。

/************************************************************************

  * 版权所有 (C)2008, 陈彦旭。
  *
  * 文件名称: MyThread.h
  * 内容摘要: 线程资源管理和使用
  * 其它说明:
  * 当前版本: 1.0.0.0
  * 作    者: 陈彦旭
  * 完成日期: 2008年01月20日
 
************************************************************************/


#if !defined(AFX_MYTHREAD_H__D97FB306_0742_43F7_8574_581DB0FBE538__INCLUDED_)
#define AFX_MYTHREAD_H__D97FB306_0742_43F7_8574_581DB0FBE538__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//#include
#include

#include "MyMutex.h"

class CMyThread 
{
public:
 CMyThread();
 virtual ~CMyThread();

public:
 static DWORD WINAPI ThreadFunc(LPVOID pParam);

public:
 virtual BOOL AfterWork();
 virtual BOOL Work();
 virtual BOOL BeforeWork();
public:
 BOOL IsRun();
 BOOL IsPause();
 BOOL IsSuspend();
 BOOL IsExit();
 BOOL SetExitState( BOOL bState );
 BOOL SetRunState( BOOL bState );
 BOOL SetPauseState( BOOL bState );
 BOOL SetSuspendState( BOOL bState );
public:
 BOOL RunThread();
 BOOL StopThread( DWORD dwDelayTime=2000 );
 
 BOOL SuspendThread();
 BOOL ResumeThread();
 
 void SetThreadName(CString sThreadName=_T(""));
 CString GetThreadName();
 
 HANDLE GetThreadHandle();
 DWORD GetThreadID();
 BOOL CloseThread();

private:
 BOOL m_bRun;
 BOOL m_bPause;
 BOOL m_bExit;
 BOOL m_bSuspend;
 CString m_sThreadName;

 CMyMutex m_RunMutex;
 CMyMutex m_PauseMutex;
 CMyMutex m_ExitMutex;
 CMyMutex m_SuspendMutex;

 HANDLE  m_hThread;
 DWORD  m_dwThreadId;
};

#endif // !defined(AFX_MYTHREAD_H__D97FB306_0742_43F7_8574_581DB0FBE538__INCLUDED_)
// MyThread.cpp: implementation of the CMyThread class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyThread.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

typedef unsigned (__stdcall *PTHREAD_START) (void *);
#define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
 pvParam, fdwCreate, pdwThreadID)                \
 ((HANDLE) _beginthreadex(                       \
 (void *) (psa),                                 \
 (unsigned) (cbStack),                           \
 (PTHREAD_START) (pfnStartAddr),                 \
 (void *) (pvParam),                             \
 (unsigned) (fdwCreate),                         \
 (unsigned *) (pdwThreadID)))


CMyThread::CMyThread()
{
 m_bRun   = FALSE;
 m_bPause  = FALSE;
 m_bExit   = FALSE;
 m_bSuspend  = FALSE;
 
 m_sThreadName = _T("");
 m_hThread  = NULL;
 m_dwThreadId = 0;
}

CMyThread::~CMyThread()
{

}


BOOL CMyThread::AfterWork()
{
 return TRUE;
}

BOOL CMyThread::Work()
{
 return TRUE;
}

BOOL CMyThread::BeforeWork()
{
 return TRUE;
}

BOOL CMyThread::IsRun()
{
 CUseMutex mutex(&this->m_RunMutex);
 return this->m_bRun;
}

BOOL CMyThread::IsPause()
{
 CUseMutex mutex(&this->m_PauseMutex);
 return this->m_bPause;
}

BOOL CMyThread::IsSuspend()
{
 CUseMutex mutex(&this->m_SuspendMutex);
 return this->m_bSuspend;
}

BOOL CMyThread::IsExit()
{
 CUseMutex mutex(&this->m_ExitMutex);
 return this->m_bExit;
}

BOOL CMyThread::SetExitState( BOOL bState )
{
 CUseMutex mutex(&this->m_ExitMutex);
 this->m_bExit = bState;
 return TRUE;
}

BOOL CMyThread::SetRunState( BOOL bState )
{
 CUseMutex mutex(&this->m_RunMutex);
 this->m_bRun = bState;
 return TRUE;
}

BOOL CMyThread::SetPauseState( BOOL bState )
{
 CUseMutex mutex(&this->m_PauseMutex);
 this->m_bPause = bState;
 return TRUE;
}
BOOL CMyThread::SetSuspendState( BOOL bState )
{
 CUseMutex mutex(&this->m_SuspendMutex);
 this->m_bSuspend = bState;
 return TRUE;
}

BOOL CMyThread::RunThread()
{
 if (IsRun())
  return TRUE;

 this->SetExitState(FALSE);
 this->SetPauseState(FALSE);
 this->SetSuspendState(FALSE);

 this->m_hThread = chBEGINTHREADEX( NULL, 0, CMyThread::ThreadFunc, (void*)this, 0, &this->m_dwThreadId);

 if (NULL == this->m_hThread)
 { 
  WQFTRACE("%s线程创建失败errorno=%d \n", this->m_sThreadName, ::GetLastError());  
  return FALSE;
 }
 
 this->SetRunState(TRUE);

 return TRUE;
}

DWORD WINAPI CMyThread::ThreadFunc(LPVOID pParam)
{
 CMyThread* pThread = (CMyThread*)pParam;

 if (!pThread->m_hThread)
 { 
  WQFTRACE("%s线程句柄已失效! \n", pThread->m_sThreadName);
  pThread->AfterWork();
  pThread->SetRunState(FALSE);
  return 0;

 }
 
 if (!pThread->BeforeWork())
 {
  pThread->SetRunState(FALSE);
  pThread->AfterWork();
  return 0;
 }

 while (!pThread->IsExit())
 {
  if (pThread->IsPause())
  {
   Sleep(1);
  }
  else
  {
   if (!pThread->Work())
    break;
  }
 }
 pThread->AfterWork();
 pThread->SetRunState(FALSE);
 return 0;
}

BOOL CMyThread::StopThread( DWORD dwDelayTime )
{
 this->SetExitState(FALSE);
 
 DWORD dwTime = ::timeGetTime();
 while ((::timeGetTime()-dwTime) < dwDelayTime)
 {
  if (!this->IsRun())
  { 
   return TRUE;
  }
  Sleep(10);
 }

 if (this->m_hThread)
 {  
  static DWORD g_ThreadTerminateCount=0;
  if (::TerminateThread(this->m_hThread, ++g_ThreadTerminateCount))
  {
   WQFTRACE("**********强制退出%s线程! \n", this->m_sThreadName);
   WQFASSERT(FALSE);
  }
 }

 this->CloseThread();
 return TRUE;
}

BOOL CMyThread::SuspendThread()
{
 return TRUE;
}

BOOL CMyThread::ResumeThread()
{
 return TRUE;
}

void CMyThread::SetThreadName(CString sThreadName)
{
}

CString CMyThread::GetThreadName()
{
 return this->m_sThreadName;
}

HANDLE CMyThread::GetThreadHandle()
{
 return this->m_hThread;
}

DWORD CMyThread::GetThreadID()
{
 return this->m_dwThreadId;
}

BOOL CMyThread::CloseThread()
{
 if (this->m_hThread)
 {
  ::CloseHandle(this->m_hThread);
  this->m_hThread = NULL;
  this->m_dwThreadId = 0;
 }
 return TRUE;
}
/************************************************************************

  * 版权所有 (C)2008, 陈彦旭。
  *
  * 文件名称: MyProcess.h
  * 内容摘要: 线程资源管理和使用
  * 其它说明:
  * 当前版本: 1.0.0.0
  * 作    者: 陈彦旭
  * 完成日期: 2008年01月20日
 
************************************************************************/


#if !defined(AFX_MYPROCESS_H__F958678E_5978_421D_BE7E_56D6266DE6FB__INCLUDED_)
#define AFX_MYPROCESS_H__F958678E_5978_421D_BE7E_56D6266DE6FB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef BOOL (*ProcessCallback)(LPVOID lpUser, DWORD dwCommand, LPVOID lpData);
#include "MyThread.h"

typedef enum {

 PROCESS_COMMAND_BEFORE,
 PROCESS_COMMAND_PROCESS,
 PROCESS_COMMAND_AFTER,
 PROCESS_COMMAND_DELETE,

} process_command_t;

class CMyProcess  : public CMyThread
{
public:
 CMyProcess();
 virtual ~CMyProcess();

private:
 LPVOID   m_lpUser;
 ProcessCallback m_pCallback;

public:
 void SetCallback(LPVOID lpUser, ProcessCallback callback);

 virtual void DeleteData(void *pData);
 
 BOOL Init();
 BOOL ReInit();
 BOOL UnInit();
 
private:
 BOOL Work();
 BOOL BeforeWork();
 BOOL AfterWork(); 
 BOOL Process(void *pData);
};

#endif // !defined(AFX_MYPROCESS_H__F958678E_5978_421D_BE7E_56D6266DE6FB__INCLUDED_)
// MyProcess.cpp: implementation of the CMyProcess class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyProcess.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyProcess::CMyProcess()
{
 this->m_lpUser  = NULL;
 this->m_pCallback = NULL;
}

CMyProcess::~CMyProcess()
{
 this->UnInit();
}

void CMyProcess::SetCallback(LPVOID lpUser, ProcessCallback callback)
{
 this->m_lpUser  = lpUser;
 this->m_pCallback = callback; 
}

void CMyProcess::DeleteData(void *pData)
{
 this->m_pCallback(this->m_lpUser, PROCESS_COMMAND_DELETE, pData);
}

BOOL CMyProcess::Work(void)
{
 return this->Process(NULL);
}

BOOL CMyProcess::BeforeWork()
{
 return this->m_pCallback(this->m_lpUser, PROCESS_COMMAND_BEFORE, NULL);
}

BOOL CMyProcess::AfterWork()
{
 return this->m_pCallback(this->m_lpUser, PROCESS_COMMAND_AFTER, NULL);
}

BOOL CMyProcess::Init()
{
 return TRUE;
}

BOOL CMyProcess::ReInit()
{
 return TRUE;
}

BOOL CMyProcess::UnInit()
{
 return TRUE;
}

BOOL CMyProcess::Process(void* pData)
{
 return this->m_pCallback(this->m_lpUser, PROCESS_COMMAND_PROCESS, pData);
}
/************************************************************************

  * 版权所有 (C)2008, 陈彦旭。
  *
  * 文件名称: MyMutex.h
  * 内容摘要: 互斥资源管理和使用
  * 其它说明:
  * 当前版本: 1.0.0.0
  * 作    者: 陈彦旭
  * 完成日期: 2008年01月20日
 
************************************************************************/

#if !defined(AFX_MYMUTEX_H__A0C8824F_B61A_443B_83DA_7F73FC0FD481__INCLUDED_)
#define AFX_MYMUTEX_H__A0C8824F_B61A_443B_83DA_7F73FC0FD481__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMyMutex 
{
public:
 CMyMutex();
 virtual ~CMyMutex();
public:
 BOOL Lock(DWORD dwLockTime=INFINITE);
 void Unlock();
 BOOL TryLock();

private:
 CRITICAL_SECTION m_Mutex;

};

class CUseMutex
{
public:
 CUseMutex();
 CUseMutex(CMyMutex* plock, BOOL bLock=TRUE, DWORD dwLockTime=INFINITE);
 virtual ~CUseMutex();
 
public: 
 BOOL IsUseLock();
 BOOL UseLock(DWORD dwLockTime=INFINITE);
 BOOL UseLock(CMyMutex* plock, BOOL bLock, DWORD dwLockTime=INFINITE);
 void UseUnlock();
 
private:
 BOOL m_bLock;
 CMyMutex* m_pLock;
};

#endif // !defined(AFX_MYMUTEX_H__A0C8824F_B61A_443B_83DA_7F73FC0FD481__INCLUDED_)

// MyMutex.cpp: implementation of the CMyMutex class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyMutex.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyMutex::CMyMutex()
{
 ::InitializeCriticalSection(&this->m_Mutex);
}

CMyMutex::~CMyMutex()
{
 ::DeleteCriticalSection(&this->m_Mutex);
}

BOOL CMyMutex::Lock(DWORD dwLockTime)
{
 ::EnterCriticalSection(&this->m_Mutex);
 return TRUE;
}

void CMyMutex::Unlock()
{
 ::LeaveCriticalSection(&this->m_Mutex);
}

BOOL CMyMutex::TryLock()
{
#if(_WIN32_WINNT >= 0x0400)
 ::TryEnterCriticalSection(&this->m_Mutex);
#else
 ::EnterCriticalSection(&this->m_Mutex);
#endif
 return FALSE;
}

CUseMutex::CUseMutex()
{
 this->m_pLock = NULL;
 this->m_bLock = FALSE;
}

CUseMutex::CUseMutex(CMyMutex* plock, BOOL bLock, DWORD dwLockTime)
{
 this->m_pLock = plock;
 this->m_bLock = FALSE;
 
 if (this->m_pLock && bLock)
 {
  this->m_bLock = this->m_pLock->Lock(dwLockTime);
 }
}

CUseMutex::~CUseMutex()
{
 this->UseUnlock();
}
 
BOOL CUseMutex::IsUseLock()
{
 return (this->m_pLock && this->m_bLock);
}

BOOL CUseMutex::UseLock(DWORD dwLockTime)
{
 if (this->m_pLock && !this->m_bLock)
  this->m_bLock = this->m_pLock->Lock(dwLockTime);

 return this->m_bLock;
}

BOOL CUseMutex::UseLock(CMyMutex* plock, BOOL bLock, DWORD dwLockTime)

 this->UseUnlock();

 this->m_pLock = plock;
 this->m_bLock = FALSE;

 if (this->m_pLock && bLock)
  return this->m_bLock = this->m_pLock->Lock(dwLockTime);

 return FALSE;
}

void CUseMutex::UseUnlock()
{
 if (this->IsUseLock())
  this->m_pLock->Unlock();

 this->m_bLock = FALSE;
}

阅读(1241) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~