您的位置:首页 > 其它

用虚函数或者bind、function实现线程的方法

2014-05-13 13:23 330 查看
应用:Thread封装

在实现自定义的线程类时,曾经这么干过:定义虚函数run(),用户自定义的CustomThread::Thread后,自己实现run()函数就OK了。 当时觉得这么做也不错。

现在有了boost::function/boost::bind我们可以这么干:

定义一个线程类:

.h文件

01    #include <pthread.h>

02    #include <string>

03    #include <boost/function.hpp>

04    #include <boost/bind.hpp>

05    

06    using namespace std;

07    class Thread

08    {

09        typedef boost::function<void()> ThreadFun;

10        public:

11            Thread(const ThreadFun& threadFun,const string& threadName = string());

12            pid_t     getThreadId();

13            string    getThreadName();

14            int       start();

15         

16        private:

17            static void* startThread(void* thread);

18       

19        private:

20            pthread_t   m_thread; //线程句柄

21            pid_t       m_tid;    //线程ID

22            string      m_strThreadName;    //线程名称

23            bool        m_bStarted;         //线程是否启动

24            ThreadFun   m_func;             //线程处理函数

25    };

.cpp文件

01    #include "thread.h"

02    

03    Thread::Thread(const Thread::ThreadFun& threadFun, const string& threadName):

04    m_func(threadFun), m_strThreadName(threadName)

05    {

06    }

07    

08    int Thread::start()

09    {

10        m_tid = pthread_create(&m_thread, NULL, &startThread, this);

11        return 0;

12    }

13    

14    void* Thread::startThread(void* obj)

15    {

16      Thread* thread = static_cast<Thread*>(obj);

17      thread->m_func();

18      return NULL;

19    }

20    

21    pid_t Thread::getThreadId()

22    {

23        return m_tid;

24    };

25    

26    string  Thread::getThreadName()

27    {

28        return m_strThreadName;

29    }

测试程序

view source

print?

01    void ThreadProcess()

02    {

03        int count = 100;

04        for (int i = 0; i < count; i++)

05        {

06            if (i % 10 == 0)   

07            cout<<"\n";

08            cout<<i<<"\t";

09        }

10    }

11    

12    int main()

13    {

14        boost::function<void()> f;

15        f = boost::bind(&ThreadProcess);   

16            Thread thread(f, "ThreadTest");

17            thread.start();

18        sleep(1000*1000);

19        return 0;
20    }

虚函数实现:

类的定义如下:

.h文件内容如下

01
#pragma once
02
#include <windows.h>
03
/****************************************************************
04
* 文件名:Thread.h
05
* 功能:线程类定义,提供线程基类
06
* 作者:
07
* 时间:2012-2-18
08
*****************************************************************/
09
class
Thread
10
{
11
    
public
:
12
        
Thread(
void
);
13
        
~Thread(
void
);
14
 
15
        
void

start();          
//线程启动     

16
        
void

stop();           
//线程关闭
17
 
18
    
protected
:
19
        
virtual

void
* run() = 0;       

//线程运行
20
        
friend

unsigned
long
_stdcall  _thd_start(
void
*param);
//友元函数
21
 
22
   
4000
 
private
:
23
        
HANDLE

_thread;        
//线程句柄
24
        
bool

m_bStop;          
//线程是否需要停止
25
        
unsigned
long
m_threadId;  

//线程ID
26
 
27
                
 
28
};
.cpp文件格式如下

01
#include "StdAfx.h"
02
#include "Thread.h"
03
 
04
 
05
/*
06
 
*函数名:_thd_start
07
 
*功能:线程入口函数
08
 
*参数:param Thread的this指针
09
 
*/
10
unsigned
long

_stdcall _thd_start(
void
* param) 
//线程函数的定义有固定的规则
11
{
12
    
return

(
long
)((Thread*)param)->run();
13
}
14
 
15
 
16
Thread::Thread(
void
)
17
{
18
        
m_bStop =
false
;
19
        
m_threadId = 0;
20
}
21
 
22
 
23
Thread::~Thread(
void
)
24
{
25
        
stop();
26
}
27
 
28
void
Thread::start()
29
{
30
    
_thread = ::CreateThread(NULL, 0, _thd_start,
this
, 0, &m_threadId);   
//线程创建后立即运行
31
}
32
 
33
 
34
void
Thread::stop()
35
{
36
    
::CloseHandle(_thread);
37
}
使用方法如下:

01
#include "stdafx.h"
02
#include "Thread.h"
03
#include <iostream>
04
 
05
using
namespace
std;
06
 
07
class
TestCustomThread :
public
Thread
08
{
09
        
protected
:
10
            
void
* run();
11
};
12
 
13
void
* TestCustomThread::run()
14
{
15
        
for

(
int
i = 0; i < 500; i++)
16
        
{
17
            
::Sleep(200);
18
            
cout<<i<<
"  "
<<endl<<flush;
19
        
}
20
 
21
        
return

NULL;
22
}
23
 
24
int
_tmain(
int

argc, _TCHAR* argv[])
25
{
26
        
TestCustomThread customThread;
27
 
28
        
customThread.start();
29
 
30
        
 
31
        
getchar
();
32
        
return

0;
33
}
如果我们想要定义自己的线程类,继承Thread类即可,并重写run()函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐