muduo基于对象编程风格[与]面向对象编程风格对比

结论

muduo代码的实现是基于对象编程风格,使用boost bind/function,替代了mem_fun,ptr_fun,bind1st,bind2nd等函数

boost::bind的使用

示例:boost::bind能够将一个函数接口,转换为另一种函数接口

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;

class Foo
{
public:
	//void memberFunc(Foo* ,double a,int b,int c);
	//实际上是有4个参数,第一个参数默认传入的是this
	void memberFunc(double a,int b,int c)
	{
		cout<<a<<endl; // 0.5
		cout<<b<<endl; // 100
		cout<<c<<endl; // 10
	}
};
int main()
{
	Foo foo;
	boost::function<void (int, int)> fp = 
		boost::bind(&Foo::memverFunc, &foo, 0.5, _1, _2);
	fp(100,10); //等价于(&foo)->memberFunc(0.5, 100, 10);
}
注释:
  # fp是返回值为void,形参为int, int类型的函数指针类型
  boost::function<void (int, int)> fp; 

  # 将Foo的成员函数memverFunc配置成fp类型,参数分别是&foo, 0.5, _1,_2
  # bind的效果为,4个参数配置成2个参数
  boost::bind(&Foo::memverFunc, &foo, 0.5, _1, _2); 

面向对象编程风格

面向对象编程风格的实现方式:
(1) Thread中有纯虚函数Run()
(2) 具体类ConcreteThread继承Thread,并重写纯虚函数Run

#ifndef _THREAD_H_
#define _THREAD_H_ 

#include <pthread.h>

class Thread 
{
public:
  Thread();
  virtual ~Thread(); 
  
  void Start();
  void Join();
private:
  static void* Thread_routine(void* arg);
  virtual void Run() = 0; //纯虚函数,给具体类实现
  pthread_t thread_id_;
};

#endif
#include <iostream>
using namespace std;

Thread::Thread()
{
  cout<<"Thread ..."<<endl;
}
Thread::~Thread()
{
  cout<<"~Thread ..."<<endl;
}
  
void Thread::Start()
{
  pthread_create(&thread_id_,NULL,Thread_routine,this);
}
void Thread::Join()
{
  pthread_join(thread_id_,NULL);
}

void* Thread_routine(void* arg)
{
  Thread* ptr = static_cast<Thread*>(arg);
  ptr->Run();
  return NULL;
}
#include "thread.h"

#include <iostream>
using namespace std;

class ConcreteThread:public Thread //具体类,实现基类的纯虚函数Run()
{
private:
  void Run()
  {
    for(int i=0;i<5;i++)
      cout<<"i = "<<i<<endl;
  }
};

int main()
{
  Thread* thread_ = new ConcreteThread();
  thread_->Start();
  thread_->Join();
  delete thread_;

  return 0;
}

基于对象编程

基于对象编程的实现方式
(1) 创建Thread类对象的时候,必须传递线程回调函数func作为形参
(2) func函数可能的参数可能会有很多情况,可以通过boost::bind对函数进行适配,适配成typedef boost::function<void ()> ThreadFunc类型

#ifndef _THREAD_H_
#define _THREAD_H_ 

#include <pthread.h>
#include <boost/function.hpp>

typedef boost::function<void ()> ThreadFunc;
class Thread 
{
public:
  expicit Thread(const ThreadFunc& func); //创建线程时,注册ThreadFunc
  virtual ~Thread(); 
  
  void Start();
  void Join();
private:
  static void* Thread_routine(void* arg);
  void Run(); // 不再是纯虚函数,需要自己实现
  pthread_t thread_id_;
  ThreadFunc func_;
};

#endif
#include <iostream>
using namespace std;

Thread::Thread(const ThreadFunc& func):func_(func)
{
  cout<<"Thread ..."<<endl;
}
Thread::~Thread()
{
  cout<<"~Thread ..."<<endl;
}
  
void Thread::Start()
{
  pthread_create(&thread_id_,NULL,Thread_routine,this);
}
void Thread::Join()
{
  pthread_join(thread_id_,NULL);
}

void* Thread_routine(void* arg)
{
  Thread* ptr = static_cast<Thread*>(arg);
  ptr->Run();
  return NULL;
}

void Thread::Run()
{
  func_(); //实际上Run()只是回调了func_()函数
}
#include "thread.h"
#include <boost/bind.hpp>

#include <iostream>
using namespace std;

void func_1()
{
  cout<<"func_1()..."<<endl;
}
void func_2(int count)
{
  while(count--)
  {
    cout<<"func_2()..."<<endl;
  }
}
class Foo
{
public:
  void func_3(int x)
  {
    while(x--)
    {
      cout << "x = " << x <<endl;
    }
  }
};
int main()
{
  Thread* t1 = new ConcreteThread(func_1);
  t1->Start();
  t1->Join();
  
  Thread* t2 = new ConcreteThread(boost::bind(func_2, 3));
  t2->Start();
  t2->Join();

  Foo foo;
  Thread* t3 = new ConcreteThread(boost::bind(&Foo::func_3, &foo, 1000));
  t3->Start();
  t3->Join();
  
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值