面向对象编程:
面向对象也就是把对象作为“接口”暴露出去,一般内部是一个 接口 或者抽象类。
Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#ifndef _THREAD_H_
#define _THREAD_H_
#include <pthread.h>
class
Thread{ public
: Thread();
virtual
~Thread(); void
Start(); void
Join(); private
: // 纯虚函数 ²»ÐҪ
void
Run() =0 ; pthread_t threadId_ ;
};#ifndef _THREAD_H_
#define _THREAD_H_
#include <pthread.h>
class
Thread{ public
: Thread();
virtual
~Thread(); void
Start(); void
Join(); private
: // 纯虚函数 ²»ÐҪ
void
Run() =0 ; pthread_t threadId_ ;
}; |
Thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#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;
} |
Thread_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#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; } |
基于对象编程:
基于对象编程,提供出来的就不是“接口”了, 而是一个具体的类,虽然他和面向对象想一个都是起到回调的作用,但是他的回调不是通过继承来实现相应的虚函数,可以独立的函数,或者成员函数。看下面的例子就知道了。
Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#ifndef _THREAD_ #define _THREAD_ #include <pthread.h> #include <boost/function.hpp> class
Thread { public
: typedef
boost::function< void
()> ThreadFunc ; explicit
Thread( const
ThreadFunc &func) ; ~Thread(); 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include "Thread.h" #include <boost/bind.hpp> #include <iostream> using
namespace std ;
Thread::Thread(
const ThreadFunc &func_ ) : func(func_),autoDelete_( false ){ } Thread::~Thread() { cout<<__FUNCTION__<<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 ); cout<< "After delete" <<endl; } void
Thread::Run(){ func(); } void
Thread::SetAutoDelete( bool
autoDelete) { this ->autoDelete_ = autoDelete; } |
Thread_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <unistd.h> #include "Thread.h" #include <iostream> #include <boost/bind.hpp> using
namespace std ; class
NumberFunction{ public
: NumberFunction(
int count):coun_t(count){}
void
NFunction() { while (coun_t-- && coun_t>0){ cout<<__FUNCTION__<<endl; sleep(1); } } private
: int
coun_t ; }; void
threadFunc (){ int
i = 9 ; while (i--){ cout<< "threadFunc...!!" <<endl ; sleep(1); } cout<<endl ; } void
threadFunc2( int
i ){ while (i--&&i>=0){ cout<< "Threadfunc2 ...." <<endl;
sleep(1); } } int
main (){ Thread *t =
new Thread(threadFunc); Thread *t2 =
new Thread(boost::bind( threadFunc2 , 10)) ; NumberFunction foo(10) ; Thread *t3 =
new Thread( boost::bind(&NumberFunction::NFunction,&foo)) ;
t->SetAutoDelete( true ); t->Start(); t2->SetAutoDelete( true ) ; t2->Start(); t3->SetAutoDelete( true ) ; t3->Start(); t2->Join(); t->Join(); t3->Join(); int
i = 9 ; while
(i--){ cout<<__FUNCTION__<<endl; } return
0 ; } |
我们看一下下面这张图
{
public :
EchoServer(){
server.SetConnectionCallback (boost::bind( onConnection()) ;
server.SetConnectionCallback (boost::bind( onMessage() ) ;
server.SetConnectionCallback (boost::bind( onClose() ) ;
TcpServer server ;
}
}
面向对象风格:
用一个EchoServer 继承TcpServer(抽象类),然后EchoServer实现三个接口onConnection、onMessage、onClose。
基于对象风格:
用一个EchoServer包含一个TcpServer(具体类)对象,在构造函数中使用boost::bind来注册 三个成员函数 onConnection、onMessage、onClose。