0%

基于对象的线程类的封装

2020年6月3日 上午9:22

总结:

  1. 编程风格:
    1. C函数风格,注册三个全局函数到网络库,网络库通过函数指针进行回调
    2. 面向对象风格,用一个EchoServer继承TcpServer(抽象类),实现三个接口OnConnection,OnMessage,OnClose
    3. 基于对象的编程风格,用一个EchoServer包含一个TcpServer(具体类)对象,在构造函数中用boost::bind来注册三个成员函数OnConnection,OnMessage,OnClose
      1. ::要站在编程范式的角度理解:::
        1. Thread类 -> control
        2. 基于对象的类 -> logic
  2. static void* ThreadRoutine(void* arg);
    1. 为什么Thread类中要使用static成员函数?
      1. pthread_create(&threadId_, NULL, ThreadRoutine, this);
      2. 在pthread_create函数无法接受有隐藏参数this的成员函数
    2. 为什么成员函数式private:
      1. 防止用户直接在Thread_test.cpp中通过类对象去调用run,这样会造成主线程main来执行run函数,而不是新建的线程
  3. explicit Thread(const ThreadFunc& func);
    1. 在构造函数中传入logic部分,其实使用的是boost库
      1. typedef boost::function<void ()> ThreadFunc;
    2. 通过boost库的封装,将方法的传入就向变量的传入一样,并且还有对应的私有成员变量ThreadFunc func_;

1.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
64
65
66
67
68
69
70
71
#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;
}

2.Thead.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
#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_

3.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
#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_();
}