diff --git a/test/src/cpp11/CMakeLists.txt b/test/src/cpp11/CMakeLists.txt index 3c0414b..ed1f4b8 100644 --- a/test/src/cpp11/CMakeLists.txt +++ b/test/src/cpp11/CMakeLists.txt @@ -3,6 +3,7 @@ project(cpp11) message("current dir" ${CMAKE_CURRENT_SOURCE_DIR}) +# set(CMAKE_CXX_FLAGS "-fno-elide-constructors") aux_source_directory(. SOURCE) message(info ${SOURCE}) add_executable(cpp11 ${SOURCE} ) \ No newline at end of file diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index d2df55d..92d0af6 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -7,7 +7,7 @@ using namespace std; int main(){ std::cout<<"test start"< v; + //调用常规的拷贝构造函数,新建字符数组,拷贝数据 + v.push_back(str); + std::cout << "After copy, str is \"" << str << "\"\n"; + //调用移动构造函数,掏空str,掏空后,最好不要使用str + v.push_back(std::move(str)); + std::cout << "After move, str is \"" << str << "\"\n"; + std::cout << "The contents of the vector are \"" << v[0]; +} \ No newline at end of file diff --git a/test/src/cpp11/thread_usage.h b/test/src/cpp11/thread_usage.h index 8c26287..344f12c 100644 --- a/test/src/cpp11/thread_usage.h +++ b/test/src/cpp11/thread_usage.h @@ -1,4 +1,5 @@ -#include +#pragma once + #include #include #include @@ -6,8 +7,11 @@ #include #include #include +#include void TestPromiseFutureBefore(); void TestThreadDetach(); void TestLockGuard(); -int TestConditionVariable(); \ No newline at end of file +int TestConditionVariable(); +int TestOptiomization(); +int TestRValue(); \ No newline at end of file diff --git a/test/src/cpp11/threadpool.cpp b/test/src/cpp11/threadpool.cpp index 1e3f150..26c11fa 100644 --- a/test/src/cpp11/threadpool.cpp +++ b/test/src/cpp11/threadpool.cpp @@ -1,17 +1,16 @@ #include "threadpool.h" // 参考于https://www.cnblogs.com/bigosprite/p/11071462.html namespace general{ - void work(general::CThreadPool *pool) - { - } + CThreadPool::CThreadPool(int num) { this->mThreadCnt = num; + mThreads.reserve(num); for(int i = 0;i < num;i++){ - std::thread *t = new std::thread(general::work,this); - this->mThreads.push_back(t); + mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i)); } } + int CThreadPool::AddTask(Task *t){ if ( nullptr == t){ return -1; @@ -53,14 +52,15 @@ namespace general{ bool CThreadPool::Started(){ return this->mStarted; } - CThreadPool::~CThreadPool(){ + + CThreadPool::~CThreadPool(){ if(this->mStarted){ for (size_t i = 0; i != this->mThreads.size(); ++i) { - // if (mThreads[i].joinable()) - // { - // mThreads[i].join(); - // } + if (mThreads[i]->joinable()) + { + mThreads[i]->join(); + } } } }