基于对象和面向对象风格

基于对象和面向对象风格这种叫法来源于陈硕的<<Linux 多线程服务端编程:使用 muduo C++ 网络库>>一书。其具体含义如下:

  1. 基于对象风格:具体类加全局函数的设计风格。
  2. 面向对象风格:使用继承和多态的设计风格。

作者认为对于应用程序,不宜使用过多的继承,设计过于复杂的继承体系。框架可以考虑使用面向对象风格。

以下例子来源于github,本文基本搬移:

用面向对象的方法封装一个Thread类

一、实现

首先定义一个基础的Thread类:

Thread.h:
#ifndef _THREAD_H_
#define _THREAD_H_

#include <pthread.h>

class Thread
{
public:
	Thread();
	virtual ~Thread();

	void Start();
	void Join();

	void SetAutoDelete(bool autoDelete);

private:
	static void* ThreadRoutine(void* arg);
	virtual void Run() = 0;
	pthread_t threadId_;
	bool autoDelete_;
};

#endif // _THREAD_H_
Thread.cpp
#include "Thread.h"
#include <iostream>
using namespace std;
Thread::Thread() : autoDelete_(false)
{
	cout<<"Thread ..."<<endl;
}

Thread::~Thread()
{
	cout<<"~Thread ..."<<endl;
}

void Thread::Start()
{
	pthread_create(&threadId_, NULL, ThreadRoutine, this);
}

void Thread::Join()
{
	pthread_join(threadId_, NULL);
}

void* Thread::ThreadRoutine(void* arg)
{
	Thread* thread = static_cast<Thread*>(arg);
	thread->Run();
	if (thread->autoDelete_)
		delete thread;
	return NULL;
}

void Thread::SetAutoDelete(bool autoDelete)
{
	autoDelete_ = autoDelete;
}

该类中提供了构造和析构函数,析构函数使用了virtual关键字标记为虚函数,为了子类能够彻底析构对象。

该基类也提供了SetAutoDelete函数来让实例化对象在该对象线程执行完后及时地自动化析构自身,默认情况下线程的实例化对象是开启自动析构的,也就是说默认情况下线程对象调用完毕后将自动销毁自身。

该基类提供了Start函数来开启一个线程,线程的回调函数为ThradRuntine,ThreadRuntine设置为私有函数,这里将该函数设置为静态的。为什么设置为静态的是有原因的,理论上Run函数才是线程回调该调用的函数,然而Run函数是需要子类来覆写实现自己的业务逻辑,也为此Run函数使用了virtual关键字。Run函数是Thread基类的一个普通的成员函数,所以其实Run函数本质上是void Run(this)的,在形参中隐藏了this指针,然而pthread_create函数需要的是一个普通的函数,函数定义如下: void *(start_runtine)(void)。所以才需要将ThreadRuntine定义为全局或者静态的,定义为全局将将该函数全部暴露,所以定义为静态。

在ThreadRoutine中运行实际业务的Run函数,这里需要注意的是pthead_create函数传递了this指针给ThreadRuntine函数,因为该函数是静态的,它不能操作非静态的函数或者变量,所以在该函数中通过传入的this指针来调用Run函数,然后在调用完毕之后delete掉this,自动销毁自身对象。

二、测试

#include "Thread.h"
#include <unistd.h>
#include <iostream>
using namespace std;

class TestThread : public Thread
{
public:
	TestThread(int count) : count_(count)
	{
		cout<<"TestThread ..."<<endl;
	}

	~TestThread()
	{
		cout<<"~TestThread ..."<<endl;
	}

private:
	void Run()
	{
		while (count_--)
		{
			cout<<"this is a test ..."<<endl;
			sleep(1);
		}
	}

	int count_;
};

int main(void)
{
	/*
	TestThread t(5);
	t.Start();

	t.Join();
	*/

	TestThread* t2 = new TestThread(5);
	t2->SetAutoDelete(true);
	t2->Start();
	t2->Join();

	for (; ; )
		pause();

	return 0;
}

测试代码中定义了一个TestThread类,继承自Thread类,然后在自身业务实现的Run方法中每隔一秒打印一条条信息,一共count条。

在main函数中动态创建了一个TestThread对象,然后调用该线程的Start函数执行业务。

基于对象风格的Thread类封装

一、boost bind/function

boost bind/function库的出现,替代了stl中的mem_fun,ptr_fun,bind1st,bin2nd等函数。使用的一个案例如下所示:

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;
class Foo
{
public:
	void memberFunc(double d, int i, int j)
	{
		cout << d << endl;//打印0.5
		cout << i << endl;//打印100       
		cout << j << endl;//打印10
	}
};
int main()
{
	Foo foo;
	boost::function<void (int, int)> fp = boost::bind(&Foo::memberFunc, &foo, 0.5, _1, _2);
	fp(100, 200);
	boost::function<void (int, int)> fp2 = boost::bind(&Foo::memberFunc, boost::ref(foo), 0.5, _1, _2);
	fp2(55, 66);
	return 0;
}

二、基于对象风格的Thread

1、Thread类图
typedef boost::function<void ()> ThreadFunc;
2、实现

Thread.h:

#ifndef _THREAD_H_
#define _THREAD_H_

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

class Thread
{
public:
	typedef boost::function<void ()> ThreadFunc;
	explicit Thread(const ThreadFunc& func);

	void Start();
	void Join();

	void SetAutoDelete(bool autoDelete);

private:
	static void* ThreadRoutine(void* arg);
	void Run();
	ThreadFunc func_;
	pthread_t threadId_;
	bool autoDelete_;
};

#endif // _THREAD_H_

Thread.cpp:

#include "Thread.h"
#include <iostream>
using namespace std;
Thread::Thread(const ThreadFunc& func) : func_(func), autoDelete_(false)
{
}

void Thread::Start()
{
	pthread_create(&threadId_, NULL, ThreadRoutine, this);
}

void Thread::Join()
{
	pthread_join(threadId_, NULL);
}

void* Thread::ThreadRoutine(void* arg)
{
	Thread* thread = static_cast<Thread*>(arg);
	thread->Run();
	if (thread->autoDelete_)
		delete thread;
	return NULL;
}

void Thread::SetAutoDelete(bool autoDelete)
{
	autoDelete_ = autoDelete;
}

void Thread::Run()
{
	func_();
}

从上面代码看出,基于对象的Thread类内部绑定了一个ThreadFunc函数来执行用户的业务函数,Run函数本质上也就是调用了ThreadFunc函数。

测试代码:

#include "Thread.h"
#include <boost/bind.hpp>
#include <unistd.h>
#include <iostream>
using namespace std;

class Foo
{
public:
	Foo(int count) : count_(count)
	{
	}

	void MemberFun()
	{
		while (count_--)
		{
			cout<<"this is a test ..."<<endl;
			sleep(1);
		}
	}

	void MemberFun2(int x)
	{
		while (count_--)
		{
			cout<<"x="<<x<<" this is a test2 ..."<<endl;
			sleep(1);
		}
	}

	int count_;
};

void ThreadFunc()
{
	cout<<"ThreadFunc ..."<<endl;
}

void ThreadFunc2(int count)
{
	while (count--)
	{
		cout<<"ThreadFunc2 ..."<<endl;
		sleep(1);
	}
}

int main(void)
{
	Thread t1(ThreadFunc);
	Thread t2(boost::bind(ThreadFunc2, 3));
	Foo foo(3);
	Thread t3(boost::bind(&Foo::MemberFun, &foo));
	Foo foo2(3);
	Thread t4(boost::bind(&Foo::MemberFun2, &foo2, 1000));

	t1.Start();
	t2.Start();
	t3.Start();
	t4.Start();

	t1.Join();
	t2.Join();
	t3.Join();
	t4.Join();
    return 0;
}

在测试程序中先创建了一个线程类t1,t1调用ThreadFunc函数打印了一次信息;

然后程序创建了t2线程,传入函数绑定了ThreadFunc2,其中传入了参数3,然后执行三次打印信息;

在主程序中创建了Foo对象,然后分别绑定该Foo对象的两个函数,打印相关信息。

三、总结

以回射服务器EchoServer为例,该EchoServer需要在内部实现OnConnection、OnMessage和OnClose三个函数,对于三种不同风格的的编程来说:

  • C编程风格:注册三个全局函数到网络库,网路库通过函数指针来回调
  • 面向对象风格:用一个EchoServer继承TcpServer(抽象类),实现三个接口的具体业务逻辑
  • 基于对象风格:用一个EchoServer包含一个TcpServer(具体类)对象,在构造函数中用boost::bind来注册三个成员函数

例如:

class EchoServer
{
public:
	EchoServer()
   {
		server.SetConnectionCallback(boost::bind(onConnection));
		server.SetOnMessageCallback(boost::bind(onConnection));
		server.SetOnCloseCallback(boost::bind(onConnection));
   }

	void OnConnection(){...}
	void OnMessage();
	voie OnClose();

	TcpServer server;
}

参考文献:

  1. https://github.com/hujiese/Large-concurrent-serve
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值