From f571e3910643001c512e4726bcf159761d210d02 Mon Sep 17 00:00:00 2001 From: caiyuzheng <290198252@qq.com> Date: Sat, 13 Mar 2021 16:34:10 +0800 Subject: [PATCH] no message --- test/src/cpp11/cpp11_test.cpp | 2 +- test/src/cpp11/thread_usage.cpp | 19 +++++++++ test/src/cpp11/thread_usage.h | 4 +- test/src/cpp11/threadpool.cpp | 69 ++++++++++++++++++++++++--------- test/src/cpp11/threadpool.h | 1 + 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index 92d0af6..55e7f10 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"< #include #include +#include "threadpool.h" void TestPromiseFutureBefore(); void TestThreadDetach(); void TestLockGuard(); int TestConditionVariable(); int TestOptiomization(); -int TestRValue(); \ No newline at end of file +int TestRValue(); +int TestThreadPool(); \ No newline at end of file diff --git a/test/src/cpp11/threadpool.cpp b/test/src/cpp11/threadpool.cpp index 26c11fa..043e893 100644 --- a/test/src/cpp11/threadpool.cpp +++ b/test/src/cpp11/threadpool.cpp @@ -5,9 +5,9 @@ namespace general{ CThreadPool::CThreadPool(int num) { this->mThreadCnt = num; - mThreads.reserve(num); - for(int i = 0;i < num;i++){ - mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i)); + mStarted = false; + if (num < 2){ + mThreadCnt = 2; } } @@ -17,12 +17,18 @@ namespace general{ } std::unique_lock lck(this->mMutex); this->mTasks.push(t); + mCd.notify_one(); } void CThreadPool::Start(){ - + mStarted = true; + mThreads.reserve(this->mThreadCnt); + for (int i = 0; i < mThreadCnt; i++) + { + mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i)); + } } - + Task *CThreadPool::PopTask(){ std::unique_lock lk(this->mMutex); while (mTasks.empty() && mStarted) @@ -30,39 +36,66 @@ namespace general{ mCd.wait(lk); } - return this->mTasks.front(); + Task *ret = this->mTasks.front(); this->mTasks.pop(); + return ret; } void CThreadPool::Process(int id) { - } - - void CThreadPool::Stop(){ - mStarted = false; - for (size_t i = 0; i != this->mThreads.size(); ++i) + std::cout << "thread id " << id << " started " << std::endl; + while (mStarted) { - if (mThreads[i]->joinable()) + Task *task = PopTask(); + std::cout<<"wake up "<join(); + continue; + } + else + { + task->Run(); } } } + void CThreadPool::Stop() + { + { + std::lock_guard lk(mMutex); + mStarted = false; + mCd.notify_all(); + } + + for (auto &th : mThreads) + { + th->join(); + } + mStarted = false; + } + bool CThreadPool::Started(){ return this->mStarted; } CThreadPool::~CThreadPool(){ - if(this->mStarted){ - for (size_t i = 0; i != this->mThreads.size(); ++i) + std::cout << "desruction" << std::endl; + if (this->mStarted) { - if (mThreads[i]->joinable()) { - mThreads[i]->join(); + std::lock_guard lk(mMutex); + mStarted = false; + mCd.notify_all(); + } + + for (size_t i = 0; i != this->mThreads.size(); ++i) + { + if (mThreads[i]->joinable()) + { + mThreads[i]->join(); + } } } - } } } \ No newline at end of file diff --git a/test/src/cpp11/threadpool.h b/test/src/cpp11/threadpool.h index 363c7be..d155d85 100644 --- a/test/src/cpp11/threadpool.h +++ b/test/src/cpp11/threadpool.h @@ -8,6 +8,7 @@ #include #include #include +#include "threadpool.h" namespace general{ class CThreadPool;