用虚函数或者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文件内容如下
.cpp文件格式如下
使用方法如下:
如果我们想要定义自己的线程类,继承Thread类即可,并重写run()函数。
在实现自定义的线程类时,曾经这么干过:定义虚函数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 | }; |
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 | } |
相关文章推荐
- Java传统多线程的实现有两种方法,继承Thread类或者实现Runnable即可.线程启动时调用start()方法.
- Function原型上的方法-bind()实现原理(Function.prototype.bind)
- ie支持function.bind()方法实现代码
- 用c++11的bind,function,boost::condition_variable实现的一个线程安全的任务队列
- ie支持function.bind()方法实现代码
- 用Java简单实现C#的参数为Action<T> Function<T,boolean>扩展方法
- 在ASP.NET中使用IHttpHandler处理请求(如自实现AJAX)时,无法获得Session(或者说是Session 为 null)的原因及解决方法
- 实现线程的两个方法
- prototype.js中Function.prototype.bind方法浅解
- Java 有几程方法可以实现一个线程?用什么关键字修饰同步?stop()和suspend()为什么不推荐使用?
- java中俩种方法实现一个线程,以及不推荐使用stop()和suspend()方法
- 运行时中函数调用黑魔法swizzle,交换两个方法或者改变一个方法的实现
- jQuery使用bind函数实现绑定多个事件的方法
- 实现视频长时间播放而显示器不会关闭或者屏保出现的简单方法
- 利用certutil.exe实现在批处理(bat)中嵌入可执行文件或者各种媒体、图片之类二进制文件的简单方法!
- 两种方法实现MFC对话框最大化时控件也随比例最大化或者还原
- 一个方法实现一个单一的功能:one method one function
- 线程 创建和启动线程的两种方式 实现Runnable接口 继承Thread类 重写唯一方法run()
- 线程安全的单例模式的几种实现方法分享
- java实现线程的两种方法有什么区别?