之前在http://www.cnblogs.com/inevermore/p/4008572.html中采用面向对象的方式,封装了Posix的线程,那里采用的是虚函数+继承的方式,用户通过重写Thread基类的run方法,传入自己的用户逻辑。

 

现在我们采用C++11的function,将函数作为Thread类的成员,用户只需要将function对象传入线程即可,所以Thread的声明中,应该含有一个function成员变量。

类的声明如下:

#ifndef THREAD_H_
#define THREAD_H_ #include <boost/noncopyable.hpp>
#include <functional>
#include <pthread.h> class Thread : boost::noncopyable
{
public:
typedef std::function<void ()> ThreadCallback; Thread(ThreadCallback callback);
~Thread(); void start();
void join(); static void *runInThread(void *); private:
pthread_t threadId_;
bool isRunning_;
ThreadCallback callback_; //回调函数
}; #endif //THREAD_H_

那么如何开启线程?思路与之前一致,写一个static函数,用户pthread_create的第三个参数,this作为最后一个参数即可。

void Thread::start()
{
pthread_create(&threadId_, NULL, runInThread, this);
isRunning_ = true;
}

 

回调函数

 

注意在这种封装方式中,我们采用了回调函数。回调函数与普通函数的区别就是,普通函数写完由我们自己直接调用,函数调用是一种不断往上堆积的方式,而回调函数通常是我们把某一个函数传入一个“盒子”,由该盒子内的机制来调用它。

在这个例子里面,我们将function传入Thread,当Thread启动的时候,由Thread去执行function对象。

在win32编程中大量用到这种机制,我们为鼠标单击、双击等事件编写相应的函数,然后将其注册给windows系统,然后系统在我们触发各种事件的时候,根据事件的类型,调用相应的构造函数。

关于回调函数,可以参考:http://www.zhihu.com/question/19801131

以后有时间,再专门总结下回调函数。

 

完整的cpp如下:

#include "Thread.h"

Thread::Thread(ThreadCallback callback)
: threadId_(0),
isRunning_(false),
callback_(std::move(callback))
{ } Thread::~Thread()
{
if(isRunning_)
{
//detach
pthread_detach(threadId_);
}
} void Thread::start()
{
pthread_create(&threadId_, NULL, runInThread, this);
isRunning_ = true;
}
void Thread::join()
{
pthread_join(threadId_, NULL);
isRunning_ = false;
} void *Thread::runInThread(void *arg)
{
Thread *pt = static_cast<Thread*>(arg);
pt->callback_(); //调用回调函数 return NULL;
}

 

这个线程的使用方式有三种:

一是将普通函数作为回调函数

void foo()
{
while(1)
{
printf("foo\n");
sleep(1);
}
} int main(int argc, char const *argv[])
{
Thread t(&foo); t.start();
t.join(); return 0;
}

 

二是采用类的成员函数作为回调函数:

class Foo
{
public:
void foo(int i)
{
while(1)
{
printf("foo %d\n", i++);
sleep(1);
}
}
}; int main(int argc, char const *argv[])
{
Foo f;
int i = 34;
Thread t(bind(&Foo::foo, &f, i)); t.start();
t.join(); return 0;
}

最后一种是组合一个新的线程类,注意这里采用的是类的组合:

class Foo
{
public: Foo()
: thread_(bind(&Foo::foo, this))
{
} void start()
{
thread_.start();
thread_.join();
} void foo()
{
while(1)
{
printf("foo\n");
sleep(1);
}
}
private:
Thread thread_;
}; int main(int argc, char const *argv[])
{
Foo f;
f.start(); return 0;
}

有些复杂的类,还需要将三种方式加以整合,例如后面要谈到的TimerThread,里面含有一个Thread和Timer,用户将逻辑注册给Timer,然后Timer的start函数注册给Thread。

这种方式的Thread,使用灵活性相对于面向对象的风格,提高了很多。

 

基于对象和面向对象

 

这里总结几点:

面向对象依靠的是虚函数+继承,用户通过重写基类的虚函数,实现自己的逻辑。

基于对象,依赖类的组合,使用function和bind实现委托机制,更加依赖于回调函数传入逻辑。

最新文章

  1. bootstrap之强调文本的类(带颜色)
  2. 获取Linux主机的CPU、内存、主板、BIOS的信息(Centos)
  3. NLPIR_Init文本分词-总是初始化失败,false,Init ICTCLAS failed!
  4. HeadFirst设计模式
  5. 用指针将字符串a的内容复制到字符串b
  6. Ellipse
  7. 【原创】抓个Firefox的小辫子,围观群众有:Chrome、Edge、IE8-11
  8. jquery中this和event.target的区别
  9. Leetcode_58_Length of Last Word
  10. 阿里云oss挂载到linux本地文件系统
  11. [Leetcode]538. Convert BST to Greater Tree
  12. 题解-洛谷P1601 A+B Problem(高精)
  13. 感觉不错的随笔 关于C、C++的
  14. 从前端中的IOC理念理解koa中的app.use()
  15. 【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao
  16. 项目引入android-support-v7-appcompat遇到的问题,no resource found that matches the given name &#39;android:Theme.AppCompat.Light&#39;
  17. java实现urlencode
  18. App Store 审核指南
  19. H264编码 封装成MP4格式 视频流 RTP封包
  20. Linux操作系统Vim代码Tab自动补全配置

热门文章

  1. xCode中去除“Implicit declaration of function &#39;sysctl&#39; is invalid in C99” 警告
  2. 多个类的DLL封装及调用
  3. 关于js拖拽功能,拖拽元素的position:fixed;left:0;right:0;样式引起左右拖动元素会出现落后鼠标移动距离的问题
  4. C# Quartz 整理
  5. Ubuntu 搭建 ***
  6. Python的程序结构[4] -&gt; 函数/Function[2] -&gt; 匿名函数
  7. [POI2015]Kinoman
  8. NOIP 2017 赛后反思 [补档]
  9. springmvc使用StringHttpMessageConverter需要配置编码
  10. ScrollView起始位置不是最顶部的解决办法