1. 程式人生 > >muduo基於物件程式設計風格[與]面向物件程式設計風格對比

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;
}