Skip to content

Commit 2fef013

Browse files
author
learnmoreonce
committed
rename
1 parent 7515d9b commit 2fef013

26 files changed

Lines changed: 1104 additions & 0 deletions

effectivecpp/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# 忽略*.o和*.a文件
3+
*.[oa]
4+
*.[out]
5+
build #过滤整个build文件夹
6+
build/
7+
*.class #过滤所有.class文件
8+
9+
za
10+
.idea
11+
.zip
12+
orig
13+
backup
14+
15+
.idea
16+
.ieda/
17+

effectivecpp/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(effectivecpp)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -w")
5+
SET (CMAKE_C_COMPILER "/usr/bin/clang")
6+
SET(CMAKE_CXX_COMPILER "clang++")
7+
aux_source_directory(. DIR_SRCS)
8+
include_directories ("${PROJECT_SOURCE_DIR}/")
9+
10+
#add_executable(muduo_ ${SOURCE_FILES} ${DIR_SRCS})
11+
#set(SOURCE_FILES main.cpp ch2.h ch3.h ch3_cast转型.h all.h ch4.h ch5.h ch6.h ch7.h ch9.h ch10.h Thread.cpp Thread.h TestThread.h Thread_base_object.h Thread_base_object_test.h ch12.h)
12+
add_executable(effectivecpp ${DIR_SRCS} ch13.h ch14.h ch15.h ch16.h ch20.h ch21.h)
13+
target_link_libraries(effectivecpp pthread)

effectivecpp/TestThread.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
////
2+
//// Created by shen on 16-9-16.
3+
////
4+
//
5+
//#ifndef EFFECTIVECPP_TESTTHREAD_H
6+
//#define EFFECTIVECPP_TESTTHREAD_H
7+
//
8+
//#include <zconf.h>
9+
//#include "Thread.h"
10+
//
11+
//using namespace std;
12+
//namespace TestThread{
13+
// class TestThread : public Thread {
14+
// public:
15+
// TestThread(int count) : count_(count) {
16+
// cout << "testthread() , ";
17+
// printf(" pid= %x\n", pthread_self());
18+
//
19+
// };
20+
//
21+
// ~TestThread() {
22+
// cout << "~testthread() ,";
23+
// printf(" pid= %x\n",(int )pthread_self());
24+
// };
25+
//
26+
// void Run() {
27+
// while (count_-- >= 0) {
28+
// cout << "count_= " << count_ << endl;
29+
// sleep(1);
30+
// }
31+
// }
32+
//
33+
// int count_;
34+
// };
35+
//
36+
// void f() {
37+
// TestThread t(3);
38+
// t.Start(); //隐含的是this指针,相当于&t。通过基类指针指向派生类对象实现了:调用派生类对象实现的虚函数run()!
39+
// //本质是虚函数的多态。实现了虚函数具有回调功能
40+
// //t.Run()代替 t.Srart()是错误的,因为前者是在主线程中运行,而后者是新线程中。故将其设置为private
41+
// t.Join();
42+
//
43+
// sleep(2);
44+
// cout << "t1 线程已经执行完毕,线程对象呢?^|^\n\n";//线程对象的生命周期 与 线程的生命周期不一样。
45+
// //线程对象仍未被销毁!
46+
// //实现 线程执行完毕,线程对象自动销毁?
47+
//// //法一
48+
// TestThread *t2 = new TestThread(3);
49+
// t2->Start();
50+
// t2->Join();
51+
// delete t2;
52+
//
53+
// cout << "t2 执行完毕,是否已经销毁? ^|^\n\n";
54+
// sleep(5);
55+
//
56+
// // //法 2
57+
// TestThread *t3 = new TestThread(3);
58+
// t3->SetAutodelete(true);
59+
// t3->Start();
60+
// t3->Join();
61+
// cout << "t3执行完毕,是否已经销毁? ^|^ \n\n";
62+
// sleep(10);
63+
//
64+
//
65+
// }
66+
//
67+
//}
68+
//
69+
//
70+
//#endif //EFFECTIVECPP_TESTTHREAD_H

effectivecpp/Thread.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
////
2+
//// Created by shen on 16-9-16.
3+
////
4+
//
5+
//#include "Thread.h"
6+
//
7+
// Thread::Thread() {
8+
// cout << "thread() , ";
9+
// printf(" pid= %x\n", pthread_self());
10+
//}
11+
//
12+
//Thread::~Thread() {
13+
// cout << "~thread() , ";
14+
// printf(" pid= %x\n", pthread_self());
15+
//}
16+
//
17+
//void Thread::Start() {
18+
// cout << "threadid = " << threadId_ << endl;
19+
// pthread_create(&threadId_, nullptr, Thread_routine, this);
20+
//}
21+
//
22+
//void Thread::Join() {
23+
// pthread_join(threadId_, NULL);
24+
//}
25+
//
26+
//void *Thread::Thread_routine(void *arg) { //静态的成员函数,不能调用非静态的成员函数(没有this指针):即只写run()是不正确的!
27+
//
28+
// Thread *thread = static_cast<Thread *>(arg); //派生类的指针,强制转换为基类指针。目的:基类指针 指向派生类对象,多态!
29+
// thread->Run();
30+
// //printf("child pid= %x \n", pthread_self());
31+
// if(thread->autodelete_)
32+
// delete thread;
33+
// return NULL;
34+
//}
35+
//
36+
//void Thread::SetAutodelete(bool autodelete) {
37+
// autodelete_=autodelete;
38+
//};
39+
//
40+
//

effectivecpp/Thread.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
////
2+
//// Created by shen on 16-9-16.
3+
////
4+
//
5+
//#ifndef EFFECTIVECPP_THREAD_H
6+
//#define EFFECTIVECPP_THREAD_H
7+
//
8+
//#include <pthread.h>
9+
//#include <iostream>
10+
//using namespace std;
11+
//
12+
//class Thread{
13+
//public:
14+
// Thread();
15+
// virtual ~Thread();
16+
// void Start();
17+
// void Join();
18+
// void SetAutodelete(bool autodelete);
19+
//
20+
//private:
21+
// virtual void Run()=0; //run是普通的虚成员函数,隐含的第一个参数是Thread*(this),调用的时候是 this->run();
22+
// static void *Thread_routine (void *arg);
23+
// pthread_t threadId_;
24+
// bool autodelete_= false;
25+
//};
26+
//
27+
//
28+
//
29+
//#endif //EFFECTIVECPP_THREAD_H

effectivecpp/Thread_base_object.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by shen on 16-9-16.
3+
//
4+
5+
#ifndef EFFECTIVECPP_THREAD_BASE_OBJECT_H
6+
#define EFFECTIVECPP_THREAD_BASE_OBJECT_H
7+
8+
#include <pthread.h>
9+
#include <iostream>
10+
#include <boost/function.hpp>
11+
12+
#include <boost/bind.hpp>
13+
14+
using namespace std;
15+
using namespace boost;
16+
typedef boost::function<void()> ThreadFunc;
17+
18+
//基于对象的编程思想
19+
class Thread {
20+
public:
21+
22+
explicit Thread(const ThreadFunc &func);
23+
24+
~Thread();
25+
26+
void Start();
27+
28+
void Join();
29+
30+
void SetAutodelete(bool autodelete);
31+
32+
private:
33+
void Run();//没有虚函数,不是继承关系,而是注册,回调!
34+
35+
static void *Thread_routine(void *arg);
36+
37+
pthread_t threadId_;
38+
ThreadFunc func_;
39+
bool autodelete_ = false;
40+
};
41+
42+
#endif //EFFECTIVECPP_THREAD_BASE_OBJECT_H
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// Created by shen on 16-9-16.
3+
//
4+
#include "Thread_base_object.h"
5+
#include <functional>
6+
#ifndef EFFECTIVECPP_THREAD_BASE_OBJECT_TEST_H
7+
#define EFFECTIVECPP_THREAD_BASE_OBJECT_TEST_H
8+
9+
10+
Thread::Thread(const ThreadFunc &func) : func_(func), autodelete_(false) {
11+
cout << "thread(),";
12+
printf(" pid= %x\n", pthread_self());
13+
};
14+
15+
Thread::~Thread() {
16+
cout << "~thread(),";
17+
printf(" pid= %x\n", pthread_self());
18+
}
19+
20+
void *Thread::Thread_routine(void *arg) {
21+
Thread *thread = static_cast<Thread *>(arg);
22+
thread->Run();
23+
cout << "Thread_routine pid= " << pthread_self() << endl;
24+
if (thread->autodelete_) delete thread;
25+
return NULL;
26+
}
27+
28+
void Thread::Run() { func_(); }
29+
30+
void Thread::SetAutodelete(bool autodelete) {
31+
autodelete_ = autodelete;
32+
};
33+
34+
void Thread::Start() {
35+
cout << "threadid = " << threadId_ << endl;
36+
pthread_create(&threadId_, nullptr, Thread_routine, this);
37+
}
38+
39+
void Thread::Join() {
40+
pthread_join(threadId_, NULL);
41+
}
42+
43+
void ThreadFunc1() {
44+
cout << "func...\n";
45+
}
46+
47+
void ThreadFunc2(int count) {
48+
while (count-- >= 0) {
49+
cout << " func2..." << count << endl;
50+
sleep(1);
51+
}
52+
}
53+
54+
55+
class Foo {
56+
public:
57+
Foo(int count) : count_(count) { cout << "Foo()...\n"; }
58+
59+
void MemberFun() {
60+
while (count_-- >= 0) {
61+
cout << " func3..." << count_ << endl;
62+
sleep(1);
63+
}
64+
}
65+
~Foo(){cout<<"~Foo...\n";}
66+
67+
private:
68+
int count_;
69+
};
70+
71+
namespace Thread_baseon_object {
72+
void f() {
73+
Thread t1(ThreadFunc1);
74+
t1.Start();
75+
//基于对象的编程思想
76+
Thread t2(bind(ThreadFunc2, 3));
77+
t2.Start();
78+
t1.Join();
79+
t2.Join();
80+
cout<<"---\n";
81+
Foo foo(3);
82+
Thread t3(boost::bind(&Foo::MemberFun,&foo));
83+
t3.Start();
84+
t3.Join();
85+
}
86+
}
87+
#endif //EFFECTIVECPP_THREAD_BASE_OBJECT_TEST_H

effectivecpp/all.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Created by shen on 16-9-15.
3+
//
4+
5+
#ifndef EFFECTIVECPP_ALL_H
6+
#define EFFECTIVECPP_ALL_H
7+
8+
using namespace std;
9+
#include <iostream>
10+
#include <vector>
11+
#include <string>
12+
#include <list>
13+
#endif //EFFECTIVECPP_ALL_H

effectivecpp/ch1.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Created by shen on 16-9-14.
3+
//
4+
5+
#ifndef EFFECTIVECPP_CH11_H
6+
#define EFFECTIVECPP_CH11_H
7+
8+
#endif //EFFECTIVECPP_CH11_H

effectivecpp/ch10.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by shen on 16-9-15.
3+
//
4+
5+
#ifndef EFFECTIVECPP_CH10_H
6+
#define EFFECTIVECPP_CH10_H
7+
8+
#include "all.h"
9+
10+
namespace ch10 {
11+
12+
void f() {
13+
int x, y, z;
14+
x = y = z = 15;//等价
15+
x = (y = (x = 15));
16+
//=操作符返回指向左侧的引用reference
17+
}
18+
19+
class Widget{
20+
public:
21+
Widget(){cout<<"widget() \n";}
22+
Widget&operator=(const Widget& rhs){
23+
cout<<"= copy \n";
24+
return * this;
25+
}
26+
};
27+
}
28+
#endif //EFFECTIVECPP_CH10_H

0 commit comments

Comments
 (0)