面向对象与基于对象 学习记录 thread举例

/********************************************************************/
* @file
* @author def< qq group: 324164944 >
* @blog http://www.cnblogs.com/itdef/
* @brief
/********************************************************************/

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
/*******************************************************************************
* @file
* @author def< qq group: 324164944 >
* @blog  http://www.cnblogs.com/itdef/
* @brief
/*******************************************************************************/
 
#include "stdafx.h"
#include <iostream>
#include <windows.h>
 
using namespace std;
 
class CThread{
public:
    CThread();
    virtual ~CThread();
    bool Start();
    void Join();
    static DWORD WINAPI ThreadProc( LPVOID lpParameter);
    virtual void Run() = 0;
private:   
    HANDLE  hThread_;
    DWORD   dwThreadId_;
};
 
CThread::CThread():
hThread_(NULL),dwThreadId_(0)
{
    cout << "Thread ..." << endl;
}
 
CThread::~CThread()
{
    if(hThread_ != NULL)
        CloseHandle(hThread_);
    cout << "~Thread ..." << endl;
}
 
bool CThread::Start()
{
    bool bRet = false;
    hThread_ = CreateThread(
        NULL,              // default security attributes
        0,                 // use default stack size 
        ThreadProc,          // thread function
        this,             // argument to thread function
        0,                 // use default creation flags
        &dwThreadId_);   // returns the thread identifier
 
    if(hThread_)
    {
        bRet = true;
    }
    return bRet;
}
 
void CThread::Join()
{
    WaitForSingleObject(hThread_,3000);
}
 
DWORD CThread::ThreadProc( LPVOID lpParameter)
{
    CThread* thread = static_cast<CThread*>(lpParameter);
    thread->Run();
    return NULL;
}
 
class CMyThread:public CThread
{
public:
    void Run(){ cout << "my thread..." << endl;}
};
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
  CMyThread thread;
  thread.Start();  
  thread.Join();
  return 0;
}

  

 

基类是最基本的几个元素 线程ID 创建进程的函数start 运行指定的线程函数run  以及等待函数join()

使用的时候直接继承 在run函数中执行自己想执行的线程处理即可。

 

基于对象则未使用继承等特性,使用bind function这对利器 来实现回调

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
#include <windows.h>
#include <iostream>
#include <boost/function.hpp>
 
class CThread
{
public:
    typedef boost::function<void ()> ThreadFunc;
    explicit CThread(const ThreadFunc& func);
    ~CThread();
 
    void Start();
    void Join();
private:
    static DWORD WINAPI ThreadProc(LPVOID arg);
    void Run();
    ThreadFunc func_;
    HANDLE hThread_;
 
};
 
void CThread::Start()
{
    hThread_ = CreateThread(
        NULL,              // default security attributes
        0,                 // use default stack size
        ThreadProc,          // thread function
        this,             // argument to thread function
        0,                 // use default creation flags
        NULL);   // returns the thread identifier
}
 
 
CThread::CThread(const ThreadFunc& func):
hThread_(NULL),func_(func)
{
    std::cout << "CThread()..." << std::endl;
}
 
void CThread::Join()
{
    WaitForSingleObject(hThread_,3000);
}
 
CThread::~CThread()
{
    if(hThread_)
        CloseHandle(hThread_);
    std::cout << "~CThread()..." << std::endl;
}
 
void CThread::Run()
{
    func_();
}
 
 
DWORD CThread::ThreadProc(LPVOID arg)
{
    CThread* thread = static_cast<CThread*>(arg);
    thread->Run();
    return NULL;
}
 
 
//======================================
 
void ThreadFunc()
{
    std::cout << "Enter thread function ...." << std::endl;
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    CThread thread(ThreadFunc);
    thread.Start();
    thread.Join();
    return 0;
}

  

 

posted on   itdef  阅读(191)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示