From 8edbbbc5a21c015589ee04a31f58b69df18c6fc2 Mon Sep 17 00:00:00 2001 From: Light-City <455954986@qq.com> Date: Tue, 5 Nov 2019 16:56:07 +0800 Subject: [PATCH] update --- ..gitignore.un~ | Bin 0 -> 523 bytes .com.sh.un~ | Bin 0 -> 3182 bytes .gitignore | 1 + .gitignore~ | 0 README.md | 76 +++++++----- {:: => basic_content/::}/:: | Bin {:: => basic_content/::}/::.cpp | 0 {:: => basic_content/::}/README.md | 0 .../abstract}/README.md | 0 .../abstract}/abstract.cpp | 0 .../abstract}/derived_full.cpp | 0 .../abstract}/interesting_facts1.cpp | 0 .../abstract}/interesting_facts2.cpp | 0 .../abstract}/interesting_facts3.cpp | 0 .../abstract}/interesting_facts4.cpp | 0 .../abstract}/interesting_facts5.cpp | 0 .../abstract}/pure_virtual.cpp | 0 {abstract => basic_content/abstract}/test.cpp | 0 {assert => basic_content/assert}/README.md | 0 {assert => basic_content/assert}/assert | Bin {assert => basic_content/assert}/assert.c | 0 {assert => basic_content/assert}/iga | Bin .../assert}/ignore_assert.c | 0 {bit => basic_content/bit}/README.md | 0 {bit => basic_content/bit}/learn.cpp | 0 {c_poly => basic_content/c_poly}/README.md | 0 {c_poly => basic_content/c_poly}/c++_examp | Bin .../c_poly}/c++_examp.cpp | 0 {c_poly => basic_content/c_poly}/c_examp | Bin {c_poly => basic_content/c_poly}/c_examp.c | 0 {const => basic_content/const}/README.md | 0 .../class_const/c++11_example/apple.cpp | 0 .../const}/class_const/c++11_example/main | Bin .../const}/class_const/c++11_example/main.cpp | 0 .../class_const/first_example/apple.cpp | 0 .../const}/class_const/first_example/main.cpp | 0 .../class_const/overload_example/apple.cpp | 0 .../const}/class_const/overload_example/main | Bin .../class_const/overload_example/main.cpp | 0 .../class_const/static_example/apple.cpp | 0 .../const}/class_const/static_example/main | Bin .../class_const/static_example/main.cpp | 0 .../const}/const_function.cpp | 0 {const => basic_content/const}/const_num.cpp | 0 .../const}/extern_const/const_file1.cpp | 0 .../const}/extern_const/const_file2.cpp | 0 .../const}/extern_const/file1.cpp | 0 .../const}/extern_const/file2.cpp | 0 .../funciton_const/condition1/condition1.cpp | 0 .../funciton_const/condition1/condition2.cpp | 0 .../funciton_const/condition1/condition3.cpp | 0 .../funciton_const/condition2/condition1 | Bin .../funciton_const/condition2/condition1.cpp | 0 .../funciton_const/condition2/condition2.cpp | 0 .../funciton_const/condition2/condition3 | Bin .../funciton_const/condition2/condition3.cpp | 0 .../funciton_const/condition3/condition1 | Bin .../funciton_const/condition3/condition1.cpp | 0 .../decltype}/README.md | 0 {decltype => basic_content/decltype}/decltype | Bin .../decltype}/decltype.cpp | 0 {enum => basic_content/enum}/README.md | 0 {enum => basic_content/enum}/classic_practice | Bin .../enum}/classic_practice.cpp | 0 .../enum}/tradition_color.cpp | 0 .../explicit}/README.md | 0 {explicit => basic_content/explicit}/explicit | Bin .../explicit}/explicit.cpp | 0 {extern => basic_content/extern}/README.md | 0 .../extern}/extern_c++/add.c | 0 .../extern}/extern_c++/add.cpp | 0 .../extern}/extern_c++/add.h | 0 .../extern}/extern_c++/add.o | Bin .../extern}/extern_c++/main | Bin .../extern}/extern_c/add.c | 0 .../extern}/extern_c/add.cpp | 0 .../extern}/extern_c/add.h | 0 .../extern}/extern_c/add.o | Bin .../extern}/extern_c/main | Bin {friend => basic_content/friend}/README.md | 0 {friend => basic_content/friend}/friend_class | Bin .../friend}/friend_class.cpp | 0 {friend => basic_content/friend}/friend_func | Bin .../friend}/friend_func.cpp | 0 .../func_pointer}/func1 | Bin .../func_pointer}/func_pointer.cpp | 0 {img => basic_content/img}/wechat.jpg | Bin {inline => basic_content/inline}/README.md | 0 {inline => basic_content/inline}/inline | Bin {inline => basic_content/inline}/inline.cpp | 0 {inline => basic_content/inline}/inline.h | 0 .../inline}/inline_virtual.cpp | 0 {inline => basic_content/inline}/iv | Bin {macro => basic_content/macro}/README.md | 0 {macro => basic_content/macro}/do_while | Bin {macro => basic_content/macro}/do_while.cpp | 0 {macro => basic_content/macro}/sig_examp | Bin {macro => basic_content/macro}/sig_examp.cpp | 0 .../pointer_refer}/README.md | 0 .../pointer_refer}/copy_construct | Bin .../pointer_refer}/copy_construct.cpp | 0 .../pointer_refer}/effec | Bin .../pointer_refer}/effec.cpp | 0 {sizeof => basic_content/sizeof}/README.md | 0 .../sizeof}/blackclass.cpp | 0 {sizeof => basic_content/sizeof}/genA.cpp | 0 {sizeof => basic_content/sizeof}/geninhe.cpp | 0 {sizeof => basic_content/sizeof}/moreinhe.cpp | 0 {sizeof => basic_content/sizeof}/morevir.cpp | 0 {sizeof => basic_content/sizeof}/static.cpp | 0 {sizeof => basic_content/sizeof}/virinhe.cpp | 0 .../sizeof}/virmoreinhe.cpp | 0 {static => basic_content/static}/README.md | 0 {static => basic_content/static}/demo | Bin .../static}/nostatic_class.cpp | 0 .../static}/static_class.cpp | 0 .../static}/static_demo.cpp | 0 .../static}/static_error_variable.cpp | 0 .../static}/static_funciton.cpp | 0 .../static}/static_variable.cpp | 0 {struct => basic_content/struct}/README.md | 0 {struct => basic_content/struct}/ext | Bin .../struct}/ext_struct_func.cpp | 0 {struct => basic_content/struct}/sf | Bin {struct => basic_content/struct}/stff | Bin {struct => basic_content/struct}/struct_func | Bin .../struct}/struct_func.c | 0 .../struct}/struct_func.cpp | 0 .../struct}/struct_func_func.cpp | 0 {struct => basic_content/struct}/stu | Bin .../struct_class}/README.md | 0 {this => basic_content/this}/README.md | 0 .../this}/img/constthis.png | Bin {this => basic_content/this}/img/genthis.png | Bin .../this}/img/thiscontrust.png | Bin {this => basic_content/this}/person | Bin {this => basic_content/this}/person.cpp | 0 {union => basic_content/union}/README.md | 0 {union => basic_content/union}/union | Bin {union => basic_content/union}/union.cpp | 0 {using => basic_content/using}/README.md | 0 {using => basic_content/using}/derived_base | Bin .../using}/derived_base.cpp | 0 {using => basic_content/using}/using_derived | Bin .../using}/using_derived.cpp | 0 {using => basic_content/using}/using_global | Bin .../using}/using_global.cpp | 0 {using => basic_content/using}/using_typedef | Bin .../using}/using_typedef.cpp | 0 {virtual => basic_content/virtual}/README.md | 0 {virtual => basic_content/virtual}/set1/emp | Bin .../virtual}/set1/emp.cpp | 0 .../virtual}/set2/default_arg.cpp | 0 .../virtual}/set3/copy_consrtuct.cpp | 0 .../virtual}/set3/full_virde.cpp | 0 .../virtual}/set3/inline_virtual.cpp | 0 .../virtual}/set3/static_error.cpp | 0 .../virtual}/set3/vir_con.cpp | 0 .../virtual}/set3/vir_de.cpp | 0 .../virtual}/set3/virtual_function.cpp | 0 .../virtual}/set3/virtual_function1.cpp | 0 .../virtual}/set3/virtual_inline.cpp | 0 {virtual => basic_content/virtual}/set4/rtti | Bin .../virtual}/set4/rtti.cpp | 0 .../virtual}/set4/warn_rtti.cpp | 0 .../volatile}/README.md | 0 .../volatile}/noopt_vola.cpp | 0 {volatile => basic_content/volatile}/nv | Bin .../volatile}/volatile.cpp | 0 .../vptr_vtable}/README.md | 0 .../vptr_vtable}/img/base.jpg | Bin .../vptr_vtable}/vptr1.cpp | 0 concurrency_v1/chapter1/1_helloworld.cpp | 20 ++++ concurrency_v1/chapter2/2.1_basic.cpp | 108 ++++++++++++++++++ concurrency_v1/chapter2/2.2_transfer.cpp | 27 +++++ concurrency_v1/chapter2/2.3_ownership.cpp | 78 +++++++++++++ concurrency_v1/chapter2/2.4_runtime.cpp | 60 ++++++++++ concurrency_v1/chapter2/2_5_id.cpp | 34 ++++++ effective_c++/2.cpp | 50 ++++++++ 179 files changed, 428 insertions(+), 26 deletions(-) create mode 100644 ..gitignore.un~ create mode 100644 .com.sh.un~ create mode 100644 .gitignore create mode 100644 .gitignore~ rename {:: => basic_content/::}/:: (100%) rename {:: => basic_content/::}/::.cpp (100%) rename {:: => basic_content/::}/README.md (100%) rename {abstract => basic_content/abstract}/README.md (100%) rename {abstract => basic_content/abstract}/abstract.cpp (100%) rename {abstract => basic_content/abstract}/derived_full.cpp (100%) rename {abstract => basic_content/abstract}/interesting_facts1.cpp (100%) rename {abstract => basic_content/abstract}/interesting_facts2.cpp (100%) rename {abstract => basic_content/abstract}/interesting_facts3.cpp (100%) rename {abstract => basic_content/abstract}/interesting_facts4.cpp (100%) rename {abstract => basic_content/abstract}/interesting_facts5.cpp (100%) rename {abstract => basic_content/abstract}/pure_virtual.cpp (100%) rename {abstract => basic_content/abstract}/test.cpp (100%) rename {assert => basic_content/assert}/README.md (100%) rename {assert => basic_content/assert}/assert (100%) rename {assert => basic_content/assert}/assert.c (100%) rename {assert => basic_content/assert}/iga (100%) rename {assert => basic_content/assert}/ignore_assert.c (100%) rename {bit => basic_content/bit}/README.md (100%) rename {bit => basic_content/bit}/learn.cpp (100%) rename {c_poly => basic_content/c_poly}/README.md (100%) rename {c_poly => basic_content/c_poly}/c++_examp (100%) rename {c_poly => basic_content/c_poly}/c++_examp.cpp (100%) rename {c_poly => basic_content/c_poly}/c_examp (100%) rename {c_poly => basic_content/c_poly}/c_examp.c (100%) rename {const => basic_content/const}/README.md (100%) rename {const => basic_content/const}/class_const/c++11_example/apple.cpp (100%) rename {const => basic_content/const}/class_const/c++11_example/main (100%) rename {const => basic_content/const}/class_const/c++11_example/main.cpp (100%) rename {const => basic_content/const}/class_const/first_example/apple.cpp (100%) rename {const => basic_content/const}/class_const/first_example/main.cpp (100%) rename {const => basic_content/const}/class_const/overload_example/apple.cpp (100%) rename {const => basic_content/const}/class_const/overload_example/main (100%) rename {const => basic_content/const}/class_const/overload_example/main.cpp (100%) rename {const => basic_content/const}/class_const/static_example/apple.cpp (100%) rename {const => basic_content/const}/class_const/static_example/main (100%) rename {const => basic_content/const}/class_const/static_example/main.cpp (100%) rename {const => basic_content/const}/const_function.cpp (100%) rename {const => basic_content/const}/const_num.cpp (100%) rename {const => basic_content/const}/extern_const/const_file1.cpp (100%) rename {const => basic_content/const}/extern_const/const_file2.cpp (100%) rename {const => basic_content/const}/extern_const/file1.cpp (100%) rename {const => basic_content/const}/extern_const/file2.cpp (100%) rename {const => basic_content/const}/funciton_const/condition1/condition1.cpp (100%) rename {const => basic_content/const}/funciton_const/condition1/condition2.cpp (100%) rename {const => basic_content/const}/funciton_const/condition1/condition3.cpp (100%) rename {const => basic_content/const}/funciton_const/condition2/condition1 (100%) rename {const => basic_content/const}/funciton_const/condition2/condition1.cpp (100%) rename {const => basic_content/const}/funciton_const/condition2/condition2.cpp (100%) rename {const => basic_content/const}/funciton_const/condition2/condition3 (100%) rename {const => basic_content/const}/funciton_const/condition2/condition3.cpp (100%) rename {const => basic_content/const}/funciton_const/condition3/condition1 (100%) rename {const => basic_content/const}/funciton_const/condition3/condition1.cpp (100%) rename {decltype => basic_content/decltype}/README.md (100%) rename {decltype => basic_content/decltype}/decltype (100%) rename {decltype => basic_content/decltype}/decltype.cpp (100%) rename {enum => basic_content/enum}/README.md (100%) rename {enum => basic_content/enum}/classic_practice (100%) rename {enum => basic_content/enum}/classic_practice.cpp (100%) rename {enum => basic_content/enum}/tradition_color.cpp (100%) rename {explicit => basic_content/explicit}/README.md (100%) rename {explicit => basic_content/explicit}/explicit (100%) rename {explicit => basic_content/explicit}/explicit.cpp (100%) rename {extern => basic_content/extern}/README.md (100%) rename {extern => basic_content/extern}/extern_c++/add.c (100%) rename {extern => basic_content/extern}/extern_c++/add.cpp (100%) rename {extern => basic_content/extern}/extern_c++/add.h (100%) rename {extern => basic_content/extern}/extern_c++/add.o (100%) rename {extern => basic_content/extern}/extern_c++/main (100%) rename {extern => basic_content/extern}/extern_c/add.c (100%) rename {extern => basic_content/extern}/extern_c/add.cpp (100%) rename {extern => basic_content/extern}/extern_c/add.h (100%) rename {extern => basic_content/extern}/extern_c/add.o (100%) rename {extern => basic_content/extern}/extern_c/main (100%) rename {friend => basic_content/friend}/README.md (100%) rename {friend => basic_content/friend}/friend_class (100%) rename {friend => basic_content/friend}/friend_class.cpp (100%) rename {friend => basic_content/friend}/friend_func (100%) rename {friend => basic_content/friend}/friend_func.cpp (100%) rename {func_pointer => basic_content/func_pointer}/func1 (100%) rename {func_pointer => basic_content/func_pointer}/func_pointer.cpp (100%) rename {img => basic_content/img}/wechat.jpg (100%) rename {inline => basic_content/inline}/README.md (100%) rename {inline => basic_content/inline}/inline (100%) rename {inline => basic_content/inline}/inline.cpp (100%) rename {inline => basic_content/inline}/inline.h (100%) rename {inline => basic_content/inline}/inline_virtual.cpp (100%) rename {inline => basic_content/inline}/iv (100%) rename {macro => basic_content/macro}/README.md (100%) rename {macro => basic_content/macro}/do_while (100%) rename {macro => basic_content/macro}/do_while.cpp (100%) rename {macro => basic_content/macro}/sig_examp (100%) rename {macro => basic_content/macro}/sig_examp.cpp (100%) rename {pointer_refer => basic_content/pointer_refer}/README.md (100%) rename {pointer_refer => basic_content/pointer_refer}/copy_construct (100%) rename {pointer_refer => basic_content/pointer_refer}/copy_construct.cpp (100%) rename {pointer_refer => basic_content/pointer_refer}/effec (100%) rename {pointer_refer => basic_content/pointer_refer}/effec.cpp (100%) rename {sizeof => basic_content/sizeof}/README.md (100%) rename {sizeof => basic_content/sizeof}/blackclass.cpp (100%) rename {sizeof => basic_content/sizeof}/genA.cpp (100%) rename {sizeof => basic_content/sizeof}/geninhe.cpp (100%) rename {sizeof => basic_content/sizeof}/moreinhe.cpp (100%) rename {sizeof => basic_content/sizeof}/morevir.cpp (100%) rename {sizeof => basic_content/sizeof}/static.cpp (100%) rename {sizeof => basic_content/sizeof}/virinhe.cpp (100%) rename {sizeof => basic_content/sizeof}/virmoreinhe.cpp (100%) rename {static => basic_content/static}/README.md (100%) rename {static => basic_content/static}/demo (100%) rename {static => basic_content/static}/nostatic_class.cpp (100%) rename {static => basic_content/static}/static_class.cpp (100%) rename {static => basic_content/static}/static_demo.cpp (100%) rename {static => basic_content/static}/static_error_variable.cpp (100%) rename {static => basic_content/static}/static_funciton.cpp (100%) rename {static => basic_content/static}/static_variable.cpp (100%) rename {struct => basic_content/struct}/README.md (100%) rename {struct => basic_content/struct}/ext (100%) rename {struct => basic_content/struct}/ext_struct_func.cpp (100%) rename {struct => basic_content/struct}/sf (100%) rename {struct => basic_content/struct}/stff (100%) rename {struct => basic_content/struct}/struct_func (100%) rename {struct => basic_content/struct}/struct_func.c (100%) rename {struct => basic_content/struct}/struct_func.cpp (100%) rename {struct => basic_content/struct}/struct_func_func.cpp (100%) rename {struct => basic_content/struct}/stu (100%) rename {struct_class => basic_content/struct_class}/README.md (100%) rename {this => basic_content/this}/README.md (100%) rename {this => basic_content/this}/img/constthis.png (100%) rename {this => basic_content/this}/img/genthis.png (100%) rename {this => basic_content/this}/img/thiscontrust.png (100%) rename {this => basic_content/this}/person (100%) rename {this => basic_content/this}/person.cpp (100%) rename {union => basic_content/union}/README.md (100%) rename {union => basic_content/union}/union (100%) rename {union => basic_content/union}/union.cpp (100%) rename {using => basic_content/using}/README.md (100%) rename {using => basic_content/using}/derived_base (100%) rename {using => basic_content/using}/derived_base.cpp (100%) rename {using => basic_content/using}/using_derived (100%) rename {using => basic_content/using}/using_derived.cpp (100%) rename {using => basic_content/using}/using_global (100%) rename {using => basic_content/using}/using_global.cpp (100%) rename {using => basic_content/using}/using_typedef (100%) rename {using => basic_content/using}/using_typedef.cpp (100%) rename {virtual => basic_content/virtual}/README.md (100%) rename {virtual => basic_content/virtual}/set1/emp (100%) rename {virtual => basic_content/virtual}/set1/emp.cpp (100%) rename {virtual => basic_content/virtual}/set2/default_arg.cpp (100%) rename {virtual => basic_content/virtual}/set3/copy_consrtuct.cpp (100%) rename {virtual => basic_content/virtual}/set3/full_virde.cpp (100%) rename {virtual => basic_content/virtual}/set3/inline_virtual.cpp (100%) rename {virtual => basic_content/virtual}/set3/static_error.cpp (100%) rename {virtual => basic_content/virtual}/set3/vir_con.cpp (100%) rename {virtual => basic_content/virtual}/set3/vir_de.cpp (100%) rename {virtual => basic_content/virtual}/set3/virtual_function.cpp (100%) rename {virtual => basic_content/virtual}/set3/virtual_function1.cpp (100%) rename {virtual => basic_content/virtual}/set3/virtual_inline.cpp (100%) rename {virtual => basic_content/virtual}/set4/rtti (100%) rename {virtual => basic_content/virtual}/set4/rtti.cpp (100%) rename {virtual => basic_content/virtual}/set4/warn_rtti.cpp (100%) rename {volatile => basic_content/volatile}/README.md (100%) rename {volatile => basic_content/volatile}/noopt_vola.cpp (100%) rename {volatile => basic_content/volatile}/nv (100%) rename {volatile => basic_content/volatile}/volatile.cpp (100%) rename {vptr_vtable => basic_content/vptr_vtable}/README.md (100%) rename {vptr_vtable => basic_content/vptr_vtable}/img/base.jpg (100%) rename {vptr_vtable => basic_content/vptr_vtable}/vptr1.cpp (100%) create mode 100644 concurrency_v1/chapter1/1_helloworld.cpp create mode 100644 concurrency_v1/chapter2/2.1_basic.cpp create mode 100644 concurrency_v1/chapter2/2.2_transfer.cpp create mode 100644 concurrency_v1/chapter2/2.3_ownership.cpp create mode 100644 concurrency_v1/chapter2/2.4_runtime.cpp create mode 100644 concurrency_v1/chapter2/2_5_id.cpp create mode 100644 effective_c++/2.cpp diff --git a/..gitignore.un~ b/..gitignore.un~ new file mode 100644 index 0000000000000000000000000000000000000000..c3cd99e7128c255194c23491708f677a8f20a42d GIT binary patch literal 523 zcmWH`%$*;a=aT=Ffyr8A(-e#Ek3I^9EKoQrTJdJX>b0uT?Ppi9e4hR3&-4a&1_lO3 zAO-h$B?<+l#R~aFndzB%3b~2JC8L32*e;71VA(lGsGUWaAX0=GBU(p0LcOY z6NCbj5Gkm@e;{CBMv@(6QaJ>G(IE+r4hDv=5@=?FqJqJ+5gbh*HgZIOq6{8Y;AjG? z2I&|D)DHn*G%4bXCN5?Dq|7}1q{QM3XoP_x2$WM{02Fzn5e5qln$y4tQ$mg~I0JF|(ZJ+a75x!^>LyNZ3f|SIPR9K!v&Tk-Bf+7->55W;S8hM}~p(OxDo&z}Y7$FTuP`-fy OP_>4=hhW +#include +#include + +using namespace std; + +void hello() { + cout << "hello world" << endl; +} + +int main() { + thread t(hello); + t.join(); // must add this line otherwise will failed! + // 需要注意的是线程对象执行了join后就不再joinable了,所以只能调用join一次。 + return 0; +} \ No newline at end of file diff --git a/concurrency_v1/chapter2/2.1_basic.cpp b/concurrency_v1/chapter2/2.1_basic.cpp new file mode 100644 index 00000000..8af63d17 --- /dev/null +++ b/concurrency_v1/chapter2/2.1_basic.cpp @@ -0,0 +1,108 @@ +// +// Created by light on 19-11-5. +// + +#include +#include +#include +#include + +using namespace std; + +class background_task { +public: + void operator()() const { + cout << "ok" << endl; + } +}; + +void do_something(int &i) { + cout << "do_something" << endl; +} + +struct func { + int &i; + + func(int &i_) : i(i_) {} + + void operator()() { + for (unsigned j = 0; j < 1000000; ++j) { + do_something(i); // 1. 潜在访问隐患:悬空引用 + } + } +}; + +// 特殊情况下的等待 +void f() { + int some_local_state = 0; + func my_func(some_local_state); + std::thread t(my_func); + try { +// do_something_in_current_thread(); + } + catch (...) { + t.join(); // 1 + throw; + } + t.join(); // 2 +} + +// try catch 只能捕获轻量级错误,所以如需确保线程在函数之前结束——查看是否因为线程函数使用了局部变量的引用, +// 以及其他原因——而后再确定一下程序可能会退出的途径,无论正常与否,可以提供一个简洁的机制,来做解决这个问题。 + +// 一种方式是使用“资源获取即初始化方式”(RAII,Resource Acquisition Is Initialization),并且提供一个类,在析构函数中使用join(), +// std::thread支持移动的好处是可以创建thread_guard类的实例,并且拥有其线程的所有权。 +class thread_guard { + std::thread &t; +public: + explicit thread_guard(std::thread &t_) : + t(t_) {} + + ~thread_guard() { + if (t.joinable()) // 1 + { + t.join(); // 2 + } + } + + thread_guard(thread_guard const &) = delete; // 3 + thread_guard &operator=(thread_guard const &) = delete; +}; +void f1() +{ + int some_local_state=0; + func my_func(some_local_state); + std::thread t(my_func); + thread_guard g(t); +// do_something_in_current_thread(); +} // 4 +// 当线程执行到4处时,局部对象就要被逆序销毁了。因此,thread_guard对象g是第一个被销毁的, +// 这时线程在析构函数中被加入2到原始线程中。 +// 即使do_something_in_current_thread抛出一个异常,这个销毁依旧会发生。 + + +int main() { + + background_task f; +// thread t(f); // ok +// t.join(); + //声明一个名为my_threadx的函数,这个函数带有一个参数(函数指针指向没有参数并返回background_task对象的函数),返回一个std::thread对象的函数 +// thread my_thread1(background_task()); + + // 针对Most Vexing Parse问题解决如下: +// thread my_thread1((background_task())); // 多组括号 +// my_thread1.join(); +// thread my_thread2{background_task()}; // 新的初始化语法 +// my_thread2.join(); + +// thread myThread([](){ +// cout<<"ok"< +#include + +using namespace std; + +class X { +public: + void do_length_work() {}; +}; + +void process_big_object(std::unique_ptr); + +int main() { + X my_x; + thread t(&X::do_length_work, &my_x); // 1 + + + std::unique_ptr p(new X); + p->do_length_work(); + std::thread tt(process_big_object,std::move(p)); + //std::thread实例的可移动且不可复制性。不可复制保性证了在同一时间点, + // 一个std::thread实例只能关联一个执行线程;可移动性使得程序员可以自己决定,哪个实例拥有实际执行线程的所有权。 + return 0; +} \ No newline at end of file diff --git a/concurrency_v1/chapter2/2.3_ownership.cpp b/concurrency_v1/chapter2/2.3_ownership.cpp new file mode 100644 index 00000000..cdbc4c3a --- /dev/null +++ b/concurrency_v1/chapter2/2.3_ownership.cpp @@ -0,0 +1,78 @@ +// +// Created by light on 19-11-5. +// +#include +#include +#include +#include +#include + +using namespace std; + +void some_function() {} + +void some_other_function() {} + +// std::thread不支持拷贝语义。 +// std::thread支持移动语义。 + + + +// scoped_thread实例 +void do_something(int i) { + cout << i << endl; +} + +struct func { + int &i; + + func(int &i_) : i(i_) {} + + void operator()() { + for (unsigned j = 0; j < 1000000; ++j) { + do_something(i); + } + } +}; + +class scoped_thread { + std::thread t; +public: + explicit scoped_thread(std::thread t_) : // 1 + t(std::move(t_)) { + if (!t.joinable()) // 2 + throw std::logic_error("No thread"); + } + + ~scoped_thread() { + t.join(); // 3 + } + + scoped_thread(scoped_thread const &) = delete; + + scoped_thread &operator=(scoped_thread const &) = delete; +}; + +void do_work(unsigned id) {} + +void f() { + std::vector threads; + for (unsigned i = 0; i < 20; ++i) { + threads.push_back(std::thread(do_work, i)); // 产生线程 + } + std::for_each(threads.begin(), threads.end(), + std::mem_fn(&std::thread::join)); // 对每个线程调用join() +} + +int main() { +// std::thread t1(some_function); // 构造一个thread对象t1 +// std::thread t2 = std::move(t1); // 把t1 move给另外一个thread对象t2,t1不再管理之前的线程了。 +// // 这句不需要std::move(),从临时变量进行移动是自动和隐式的。调用的是operator=(std::thread&&) +// t1 = std::thread(some_other_function); +// std::thread t3; +// t3 = std::move(t2); // 把t2 move给t3 +// // 把t3 move给t1,非法。因为`t1`已经有了一个相关的线程,会调用`std::terminate()`来终止程序。 +// t1 = std::move(t3); + f(); + return 0; +} \ No newline at end of file diff --git a/concurrency_v1/chapter2/2.4_runtime.cpp b/concurrency_v1/chapter2/2.4_runtime.cpp new file mode 100644 index 00000000..9928aa9f --- /dev/null +++ b/concurrency_v1/chapter2/2.4_runtime.cpp @@ -0,0 +1,60 @@ +// +// Created by light on 19-11-5. +// +#include +#include +#include +#include +#include + +using namespace std; + +//使得每个线程具有最小数目的元素以避免过多的线程开销 +template +struct accumulate_block { + void operator()(Iterator first, Iterator last, T &result) { + result = std::accumulate(first, last, result); + } +}; + +template +T parallel_accumlate(Iterator first, Iterator last, T init) { + unsigned long const length = std::distance(first, last); + + if (!length) + return init; + + unsigned long const min_per_thread = 25; + unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread; + cout< results(num_threads); + std::vector threads(num_threads - 1); + + Iterator block_start = first; + for (unsigned long i = 0; i < (num_threads - 1); ++i) { + Iterator block_end = block_start; + std::advance(block_end, block_size); + threads[i] = std::thread(accumulate_block(), block_start, block_end, std::ref(results[i])); + block_start = block_end; + } + accumulate_block()(block_start, last, results[num_threads - 1]); + std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join)); + + return std::accumulate(results.begin(), results.end(), init); +} + + +int main() { + + vector v{3,4,5,6}; + int res=0; + cout< +#include + +using namespace std; + + +// 线程的通用标识符 +std::thread::id master_thread; + +void do_master_thread_work() { + cout << "master" << endl; +} + +void do_common_work() { + cout << "common" << endl; +} + +void some_core_part_of_algorithm() { + if (std::this_thread::get_id() == master_thread) { + do_master_thread_work(); + } + do_common_work(); +} + +int main() { + std::cout << std::this_thread::get_id() << endl; + thread t(some_core_part_of_algorithm); + t.join(); + return 0; +} \ No newline at end of file diff --git a/effective_c++/2.cpp b/effective_c++/2.cpp new file mode 100644 index 00000000..428f3b19 --- /dev/null +++ b/effective_c++/2.cpp @@ -0,0 +1,50 @@ +// +// Created by light on 19-9-11. +// + +#include + +using namespace std; +struct A { +#define p "hello" +}; + +class C { +public: + // static const 可以直接在类内部初始化 + // no-const static 只能在外面初始化 + static const int NUM = 3; //声明 + enum con { + NUM1 = 3 + }; +}; + +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +template +inline int Max(const T& a, const T& b){ + return (a>b ? a:b); +} +const int C::NUM; // 定义 +int main() { + cout << p << endl; // macro is global + C c; + cout << &c.NUM << endl; // 未定义的引用,需要定义 + + cout << C::NUM1 << endl; +// cout << &C:NUM1 << endl; //error enum no address + + int a=5, b=0; + cout<