惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
B
Blog
N
Netflix TechBlog - Medium
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
M
MIT News - Artificial intelligence
D
DataBreaches.Net
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LINUX DO - 热门话题
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
Vercel News
Vercel News
T
Tenable Blog
Security Latest
Security Latest
C
Check Point Blog
云风的 BLOG
云风的 BLOG
PCI Perspectives
PCI Perspectives
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
雷峰网
雷峰网
IT之家
IT之家
H
Hacker News: Front Page
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Last Week in AI
Last Week in AI
G
Google Developers Blog
Forbes - Security
Forbes - Security
The Register - Security
The Register - Security
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Martin Fowler
Martin Fowler
K
Kaspersky official blog
P
Proofpoint News Feed
T
Threatpost
Google Online Security Blog
Google Online Security Blog
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - hcfalan

Mybatis分页功能 安聊服务端Netty的应用 安聊系统1.0发布 VC中发送电子邮件 自绘窗口边框和标题栏 Windows下生产者-消费者问题的解法 Javascript 时间日期转换 Java Threading - Consumer&Producer Boost socket 同步编程示例(服务端,客户端) MFC CSocket的跨线程问题 用于主题检测的临时日志(54b8134e-5e4a-46fc-95af-c75ccf06d66e - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 网络电话 VC 剪贴板操作 The 1st Boost Serial Demo - hcfalan MS Proxy FTP Sample 《斯坦福大学开放课程: 编程方法学》 VC2005下编译log4cpp的修正方法 - hcfalan - 博客园 适合孩子的书籍 应用MFC开发高级应用程序(转)
一个最经典的线程类(转)
hcfalan · 2010-12-08 · via 博客园 - hcfalan

这个类源代码来源于网上流传的魔域的代码,自己翻来覆去仿写了好多遍都觉得很好用,所以拿出来与大家分享,希望别人什么时候也用得着。

.h

#ifndef THREADBASE_H

#define THREADBASE_H

#include <afxmt.h>

//#include <windows.h>

class CThreadBase

{

protected: // 构造、析构

    CThreadBase();

    virtual ~CThreadBase();

public: // 公共,由父线程调用。

    bool CreateThread(bool bRun = true); // false: 暂不运行,用 ResumeThread() 运行

    bool ResumeThread(); // return false: 失败

    // 通知子线程关闭,并阻塞 nMilliseconds 毫秒。返回true: 线程关闭成功

    bool CloseThread(long nMilliseconds = 0);

/////////////////////////////////////////////////////////////////////

protected: // 派生用

    virtual void OnInit();

    virtual bool OnProcess(); // 不需要返回DWORD

    virtual void OnDestroy();

/////////////////////////////////////////////////////////////////////

private: // 内部使用

    bool IsCloseEvent(long nMilliseconds = 0); // 检查关闭线程事件是否触发。用于 ThreadProc() 函数调用

    DWORD ThreadProc();

bool IsCreated();

protected:

    // CCriticalSection m_xCtrl; // 用于派生类中的变量共享控制

    HANDLE m_hThread;

    HANDLE m_hCloseThreadEvent;

    CCriticalSection m_xCtrl;

/////////////////////////////////////////////////////////////////////

private:

    static DWORD WINAPI TrueThreadProc(LPVOID pParam);

};

#endif // THREADBASE_H

.cpp

#include "StdAfx.h"

#include "ThreadBase.h"

CThreadBase::CThreadBase(void)

{

    m_hThread = NULL;

    m_hCloseThreadEvent = NULL;

}

CThreadBase::~CThreadBase(void)

{

    if (NULL != m_hThread)

    {

        if (::WaitForSingleObject(m_hThread,0) == WAIT_TIMEOUT)

        {

            ::TerminateThread(m_hThread,5);

        }

        ::CloseHandle(m_hThread);

        m_hThread = NULL;

    }

    if (m_hCloseThreadEvent)

    {

        ::CloseHandle(m_hCloseThreadEvent);

        m_hCloseThreadEvent = NULL;

    }

}

bool CThreadBase::CreateThread(bool bRun /* = true */)

{

    if (!IsCreated())

    {

        m_hCloseThreadEvent = ::CreateEvent(NULL,false,false,NULL);

        if (NULL == m_hCloseThreadEvent)

        {

            return false;

        }

        DWORD dwThreadID = 0;

        DWORD dwCreateFlag = bRun ? 0 : CREATE_SUSPENDED;

        m_hThread = ::CreateThread(NULL,0,TrueThreadProc,(void*)this,dwCreateFlag,&dwThreadID);

        if (NULL != m_hThread)

        {

            return true;

        }

        else

        {

            ::CloseHandle(m_hCloseThreadEvent);

            m_hCloseThreadEvent = NULL;

            return false;

        }

    }

    return false;

}

bool CThreadBase::ResumeThread()

{

    if (NULL != m_hThread)

    {

        if (::ResumeThread(m_hThread) != -1)

        {

            return false;

        }

        else

        {

            int err = GetLastError();

            ASSERT(!err);

            return true;

        }

    }

    return false;

}

bool CThreadBase::CloseThread(long nMilliseconds /* = 0 */)

{

    if (NULL != m_hThread)

    {

        ::ResumeThread(m_hThread);

        ::SetEvent(m_hCloseThreadEvent);

        if (::WaitForSingleObject(m_hThread,nMilliseconds) != WAIT_TIMEOUT)

        {

            CloseHandle(m_hThread);

            m_hThread = NULL;

            CloseHandle(m_hCloseThreadEvent);

            m_hCloseThreadEvent = NULL;

            return true;

        }

        else

        {

            return false;

        }

    }

    return true;

}

bool CThreadBase::IsCreated()

{

    return (m_hThread != NULL);

}

void CThreadBase::OnInit()

{

    // Do Nothing Here

}

bool CThreadBase::IsCloseEvent(long nMilliseconds /* = 0 */)

{

    if (::WaitForSingleObject(m_hCloseThreadEvent,nMilliseconds) != WAIT_TIMEOUT)

    {

        return true;

    }

    return false;

}

bool CThreadBase::OnProcess()

{

    // Do Nothing here

    return false;

}

void CThreadBase::OnDestroy()

{

    // Do Nothing Here

}

DWORD CThreadBase::ThreadProc()

{

    DWORD bRet = 0;

    OnInit();

    while (!IsCloseEvent())

    {

        if (!OnProcess())

        {

            bRet = 2;

            break;

        }

    }

    OnDestroy();

    return bRet;

}

DWORD WINAPI CThreadBase::TrueThreadProc(LPVOID pParam)

{

    CThreadBase *pThread = (CThreadBase*)(pParam);

    return pThread->ThreadProc();

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Minkowsky/archive/2010/06/10/5660150.aspx