跨平台线程类,需要增加宏定义
Windows平台增加宏DX_WINDOWS
Linux平台增加宏DX_LINUX
DXThread.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#ifndef __DXTHREAD_H__ #define __DXTHREAD_H__ //#define DX_WINDOWS //在WINDOWS上就打开它 //#define DX_LINUX //在LINUX 上就打开它 #ifdef DX_WINDOWS #include <windows.h> #define DX_CALLBACK WINAPI typedef HANDLE DXHANDLE; #endif #ifdef DX_LINUX #include <pthread.h> #define DX_CALLBACK typedef pthread_t DXHANDLE; #endif class CDXThread { public: CDXThread(); virtual ~CDXThread(); virtual int Run(); bool Start(); bool Join(); bool Detach(); private: static void* DX_CALLBACK RunThread(void* pParam); DXHANDLE m_hThread; void* m_pParam; unsigned long m_nRet; protected: bool m_bStart; int m_nState; }; #endif //DXThread.h |
DXThread.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#include "DXThread.h" CDXThread::CDXThread() { m_hThread = 0; m_pParam = NULL; m_nRet = 0; m_bStart = false; m_nState = 0; } CDXThread::~CDXThread() { if (m_bStart && (m_nState == 0)) { Join(); } } int CDXThread::Run() { return 0; } bool CDXThread::Start() { if (true == m_bStart) return true; bool bOK = true; m_bStart = true; #ifdef DX_WINDOWS if (NULL == (m_hThread = CreateThread(NULL, 0, (unsigned long(DX_CALLBACK*)(void*))&RunThread, this, 0, NULL))) { bOK = false; } #endif #ifdef DX_LINUX if (0 != pthread_create(&m_hThread, NULL, RunThread, this)) { bOK = false; } #endif if (!bOK) { m_bStart = false; return false; } return true; } bool CDXThread::Join() { if (!m_bStart) return false; if (m_nState == 1) return false; bool ret; m_bStart = false; #ifdef DX_WINDOWS while (1) { if (FALSE == GetExitCodeThread(m_hThread, &m_nRet)) { ret = false; break; } else { if (m_nRet == STILL_ACTIVE) { Sleep(100); continue; } CloseHandle(m_hThread); ret = true; break; } } #endif #ifdef DX_LINUX ret = (0 == pthread_join(m_hThread, (void **)&m_nRet)); #endif m_hThread = 0; return ret; } bool CDXThread::Detach() { if (!m_bStart) return false; if (m_nState == 1) return false; m_nState = 1; #ifdef DX_WINDOWS return (TRUE == CloseHandle(m_hThread)); #endif #ifdef DX_LINUX return (0 == pthread_detach(m_hThread)); #endif } void* DX_CALLBACK CDXThread::RunThread(void* pParam) { CDXThread* pThis = (CDXThread*)pParam; long nRet = pThis->Run(); return (void*)nRet; } |
测试代码test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include "DXThread.h" class CTest : public CDXThread { public: int Run() { printf("1..\n"); return 0; } }; int main() { CTest a; a.Start(); //a.Detach(); //分离线程 if (false == a.Join()) { printf("join failed!\n"); } return 0; } |
发表评论
要发表评论,您必须先登录。