win32线程简单封装
2011-06-08 23:13
316 查看
事件是比较让我满意的.
Basic.h
nonecopyable.h
====================================Mutex====================================
Mutex.h
Mutex.cpp
===================================Event=====================================
Event.h
Event.cpp
=====================================Thread=====================================
Thread.h
Thread.cpp
ThreadBase.h
Basic.h
/********************************************************************************************************************** * FILE NAME : Basic.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef Basic_h__ #define Basic_h__ #include <stdlib.h> #include <stdio.h> #include <vector> #include <map> #include <list> #include <string> #include <algorithm> #include <Windows.h> using std::vector; using std::map; using std::list; using std::string; /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /* assert */ #ifdef assert #undef assert #endif #define assert _ASSERT /* return */ #ifdef SUCCESS #undef SUCCESS #endif #define SUCCESS (0) #ifdef FAILURE #undef FAILURE #endif #define FAILURE (-1) /* common type */ typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned short ushort; typedef unsigned char uchar; /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* Basic_h__ */ /*****************************************************EOF*************************************************************
nonecopyable.h
/********************************************************************************************************************** * FILE NAME : NoneCopyable.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef NoneCopyable_h__ #define NoneCopyable_h__ /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ class CNoneCopyable { public: CNoneCopyable() {} private: CNoneCopyable(const CNoneCopyable& rhs) {} CNoneCopyable& operator=(CNoneCopyable& rhs) {} }; /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* NoneCopyable_h__ */ /*****************************************************EOF*************************************************************/
====================================Mutex====================================
Mutex.h
/********************************************************************************************************************** * FILE NAME : Mutex.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef Mutex_h__ #define Mutex_h__ /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ typedef CRITICAL_SECTION mutex_t; /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ class CMutex { public: CMutex(); virtual ~CMutex(); void Lock(); void Unlock(); protected: int acquire(); int release(); protected: mutex_t m_mutex_object; }; class CMutexGuard { public: CMutexGuard() { m_mutex_base.Lock(); } virtual ~CMutexGuard() { m_mutex_base.Unlock(); } protected: CMutex m_mutex_base; }; /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* Mutex_h__ */ /*****************************************************EOF*************************************************************/
Mutex.cpp
/********************************************************************************************************************** * FILE NAME : Mutex.cpp * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #include "ThreadBase.h" #include "Mutex.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ CMutex::CMutex() { ::InitializeCriticalSection(&m_mutex_object); } CMutex::~CMutex() { ::DeleteCriticalSection(&m_mutex_object); } void CMutex::Lock() { acquire(); } void CMutex::Unlock() { release(); } inline int CMutex::acquire() { ::EnterCriticalSection(&m_mutex_object); return 0; } inline int CMutex::release() { ::LeaveCriticalSection(&m_mutex_object); return 0; } /*****************************************************EOF*************************************************************/
===================================Event=====================================
Event.h
/********************************************************************************************************************** * FILE NAME : Event.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef Event_h__ #define Event_h__ #include "ThreadBase.h" #include "Mutex.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ class CEvent : public CNoneCopyable { public: CEvent(); virtual ~CEvent(); public: int WaitEvents(ulong MaskBits, ulong* HitBits, int Timeout = WAIT_TIMEOUT); void SetEvent(ulong SetBit); void CancelEvent(ulong CancelBit); protected: HANDLE m_event_handle[32]; ulong m_set_pattern; CMutex m_mutex; CMutex m_mutex_wait; }; /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* Event_h__ */ /*****************************************************EOF*************************************************************/
Event.cpp
/********************************************************************************************************************** * FILE NAME : Event.cpp * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #include "Event.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ CEvent::CEvent() { for(int loop = 0; loop < 32; ++loop) { m_event_handle[loop] = INVALID_HANDLE_VALUE; } } CEvent::~CEvent() { } int CEvent::WaitEvents(ulong MaskBits, ulong* HitBits, int Timeout /* = WAIT_TIMEOUT */) { int ret = FAILURE; ulong pattern_filter = 0x01; int wait_event_cnt = 0; *HitBits = 0; int array_index = 0; m_mutex_wait.Lock(); for(int loop = 0; loop < 32 && (0 != MaskBits); ++loop, pattern_filter <<= 1) { if(0 != (MaskBits & pattern_filter)) { if(INVALID_HANDLE_VALUE == m_event_handle[loop]) { m_event_handle[wait_event_cnt] = ::CreateEvent( NULL, FALSE, FALSE, NULL); assert(INVALID_HANDLE_VALUE != m_event_handle[wait_event_cnt]); } ++wait_event_cnt; } } DWORD event_ret = ::WaitForMultipleObjects( wait_event_cnt, m_event_handle, FALSE, Timeout); if(WAIT_FAILED != ret && WAIT_TIMEOUT != ret) { array_index |= ret - WAIT_OBJECT_0; } *HitBits = (MaskBits & m_set_pattern); /* * Because we have locked, * it's impossible to make signal event and wait happen * at the same time, so make it simply here! */ m_set_pattern = 0; m_mutex_wait.Unlock(); if(0 != *HitBits) { ret = SUCCESS; } return ret; } void CEvent::SetEvent(ulong SetBit) { m_mutex.Lock(); ulong pattern_filter = 0x01; m_set_pattern |= SetBit; for(int loop = 0; loop < 32; ++loop, pattern_filter <<= 1) { if(pattern_filter & m_set_pattern) { if(INVALID_HANDLE_VALUE == m_event_handle[loop]) { m_event_handle[loop] = ::CreateEvent( NULL, FALSE, FALSE, NULL); assert(INVALID_HANDLE_VALUE != m_event_handle[loop]); } else { ::SetEvent(m_event_handle[loop]); } } } m_mutex.Unlock(); } void CEvent::CancelEvent(ulong CancelBit) { m_mutex.Lock(); ulong pattern_filter = 0x01; m_set_pattern |= ~CancelBit; /* We do not delete event after cancel */ m_mutex.Unlock(); } /*****************************************************EOF*************************************************************/
=====================================Thread=====================================
Thread.h
/********************************************************************************************************************** * FILE NAME : Thread.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef Thread_h__ #define Thread_h__ #include "NoneCopyable.h" #include "ThreadBase.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ class CThread : CNoneCopyable { public: CThread(); virtual ~CThread(); virtual void StartTask(); virtual void SuspendTask(); static DWORD __stdcall ThreadProc(LPVOID lp); protected: virtual void runTask(LPVOID lp) = 0; protected: HANDLE m_thread_handle; LPVOID m_thread_info; }; /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* Thread_h__ */ /*****************************************************EOF*************************************************************/
Thread.cpp
/********************************************************************************************************************** * FILE NAME : Thread.cpp * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #include "Thread.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ CThread::CThread() { m_thread_handle = ::CreateThread( NULL, 0, &CThread::ThreadProc, this, CREATE_SUSPENDED, NULL); assert(INVALID_HANDLE_VALUE != m_thread_handle); } CThread::~CThread() { ::CloseHandle(m_thread_handle); }; DWORD __stdcall CThread::ThreadProc(LPVOID lp) { assert(NULL != lp); ((CThread*)lp)->runTask(NULL); return 0; } void CThread::StartTask() { assert(INVALID_HANDLE_VALUE != m_thread_handle); ::ResumeThread(m_thread_handle); } void CThread::SuspendTask() { assert(INVALID_HANDLE_VALUE != m_thread_handle); ::SuspendThread(m_thread_handle); } /*****************************************************EOF*************************************************************/
ThreadBase.h
/********************************************************************************************************************** * FILE NAME : ThreadBase.h * CREATE DATE : 2011/04/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #ifndef ThreadBase_h__ #define ThreadBase_h__ #include "Basic.h" #include "nonecopyable.h" /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ /********************************************************************************************************************** * Functions **********************************************************************************************************************/ /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ #endif /* ThreadBase_h__ */ /*****************************************************EOF*************************************************************/
相关文章推荐
- win32线程简单封装
- 简单的WIN32窗口封装
- 简单说说android的线程封装 zz .
- Win32 Service的简单封装
- 简单说说android的线程封装
- Win32 Service的简单封装
- Win32 Service的简单封装
- pthread简单封装 线程退出
- C入门:简单的win32线程
- 一组便于创建线程和线程池的简单封装函数
- 简单说说android的线程封装
- Win32 Service的简单封装
- 将boost::thread的某些简单功能封装成类java线程的想法。
- Netty中线程封装与管理与在游戏中的简单使用
- python使用线程封装的一个简单定时器类实例
- Win32 Service的简单封装
- python使用线程封装的一个简单定时器类实例
- 简单说说android的线程封装 zz
- 【pthread系列-11】对Pthread线程进行简单的类封装
- Windows线程简单封装