码迷,mamicode.com
首页 > 编程语言 > 详细

QThead实现模拟VC线程函数

时间:2015-03-15 15:17:50      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

// 头文件
#ifndef QTHREADBASE_H
#define QTHREADBASE_H

#include <iostream>
#include <stdlib.h>
#include <qmutex.h>
#include <Qthread.h>
#include <list>

#ifdef _MAC
#define CALLBACK    PASCAL
#define WINAPI      CDECL
#define WINAPIV     CDECL
#define APIENTRY    WINAPI
#define APIPRIVATE  CDECL
#ifdef _68K_
#define PASCAL      __pascal
#else
#define PASCAL
#endif
#elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
#define CALLBACK    __stdcall
#define WINAPI      __stdcall
#define WINAPIV     __cdecl
#define APIENTRY    WINAPI
#define APIPRIVATE  __stdcall
#define PASCAL      __stdcall
#else
#define CALLBACK
#define WINAPI
#define WINAPIV
#define APIENTRY    WINAPI
#define APIPRIVATE
#define PASCAL      pascal
#endif

class CThread: public QThread
{
public:
    CThread();
    virtual ~CThread();
public:
    // 设置等待超时
    void SetTimOut(unsigned long dwTimeOut = ULONG_MAX);
    // 获取等待超时
    unsigned long  GetTimeOut();
    // 允许线程强制退出
    void EnableTerminateThread(bool bEnable = false);
    bool IsEnableTerminateThread();
    // 线程退出标志
    bool IsQuit();
    // 设置线程退出标示
    void SetQuit(bool bQuit);
    // 线程延迟
    void ThreadSleep(unsigned long dwTime);
    // 启动线程
    bool StartThread(void (WINAPI *ThreadFun)(void* pParam) ,Priority nPriority = NormalPriority,void* ThreadParam = NULL);
    // 设置线程权限
    bool SetThreadPriority(Priority priority);
    // 恢复线程
    unsigned long ResumeThread();
    // 挂起线程
    unsigned long SuspendThread();
    // 设置线程与CPU某个核运行
    unsigned long SetThreadAffinityMask(unsigned long dwThreadAffinityMask);
    // 等待线程退出
    bool WaitQuit(unsigned long dwMilliseconds);
    // 强制杀掉线程
    bool TerminateThread();
public:
    void SetCustomData(void *lpData);
    void *GetCustomData();
private:
    void run();
private:
    void (WINAPI *FUN_ThreadFun)(void* pParam);
    void*    m_lpThreadParam;

    unsigned long m_dwTimeOut;
    // 允许强行杀线程
    bool m_bEnableForceKill;
    bool m_bAllowExit;
    // 自定用户参数
    void *m_lpCustomData;
};

#endif // QTHREADBASE_H

 

// cpp文件
#include "Thread.h"

CThread::CThread()
{
    m_dwTimeOut = ULONG_MAX;
    m_bEnableForceKill = false;
    SetQuit(false);

    m_lpCustomData = NULL;
}
CThread::~CThread()
{
    SetQuit(true);
    if(!IsEnableTerminateThread())
    {
        if(!WaitQuit(m_dwTimeOut))
        {
            TerminateThread();
        }
    }
    else
    {
        TerminateThread();
    }
}
void CThread::SetCustomData(void *lpData)
{
    m_lpCustomData = lpData;
}

void *CThread::GetCustomData()
{
    return m_lpCustomData;
}

void CThread::run()
{
    if(FUN_ThreadFun)
    {
        FUN_ThreadFun(m_lpThreadParam);
    }
}

bool CThread::WaitQuit(unsigned long dwMilliseconds)
{
    return wait(dwMilliseconds);
}
void CThread::EnableTerminateThread(bool bEnable)
{
    m_bEnableForceKill = bEnable;
}
bool CThread::IsEnableTerminateThread()
{
    return m_bEnableForceKill;
}

void CThread::SetTimOut(unsigned long dwTimeOut)
{
    m_dwTimeOut = dwTimeOut;
}
unsigned long CThread::GetTimeOut()
{
    return m_dwTimeOut;
}
bool CThread::IsQuit()
{
    return m_bAllowExit;
}
void CThread::SetQuit(bool bQuit)
{
    m_bAllowExit = bQuit;
}
void CThread::ThreadSleep(unsigned long dwTime)
{
    if( dwTime > 10)
    {
        unsigned int dwCount = 0;
        while(!IsQuit())
        {
            QThread::msleep(10);

            if(++dwCount > (unsigned int)(dwTime / 10))
            {
                break;
            }
        }
    }
    else
    {
        QThread::msleep(dwTime);
    }
}

// If the function succeeds, the return value is nonzero.
unsigned long CThread::SetThreadAffinityMask(unsigned long dwThreadAffinityMask)
{// 暂未实现
    return 0;
}
bool CThread::StartThread(void (WINAPI *ThreadFun)(void* pParam),Priority nPriority,void* ThreadParam)
{
    FUN_ThreadFun = ThreadFun;
    m_lpThreadParam = ThreadParam;

    start(nPriority);
    return true;
}
//设置优先级为高于正常
bool CThread::SetThreadPriority(Priority priority)
{
    setPriority(priority);
    return false;
}
unsigned long CThread::ResumeThread()
{// 暂未实现
    return 0xFFFFFFFF;
}
unsigned long CThread::SuspendThread()
{// 暂未实现
    return 0xFFFFFFFF;
}

// 强制杀掉线程
bool CThread::TerminateThread()
{
    terminate();

    return true;
}

 

//使用
 static void WINAPI Thread_Fun(void* wParam);
void CTest::Start()
{
  CThread *pThead = new CThread ;
   pThead ->SetCustomData(this);
   pThead ->SetTimOut(2000);
   pThead ->StartThread(Thread_Fun,CThread::NormalPriority,pThead );
} 

void WINAPI CTest::Thread_Fun(void* wParam) 
{ 
  CThread *pThread = (CThread*)wParam; 
  if(pThread) 
  { 
    CTest*pFrame = (CTest*)pThread->GetCustomData(); 
    if(pFrame) 
    { 
      while(!pThread->IsQuit()) 
      { 
        // 处理指令函数  
        pThread->ThreadSleep(1); 
      } 
    } 
  }
}

 

QThead实现模拟VC线程函数

标签:

原文地址:http://www.cnblogs.com/jason-pmsoft/p/4339718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!