no message

master
caiyuzheng 2021-03-13 16:34:10 +08:00
parent 9c139d745a
commit f571e39106
5 changed files with 75 additions and 20 deletions

View File

@ -7,7 +7,7 @@ using namespace std;
int main(){ int main(){
std::cout<<"test start"<<endl; std::cout<<"test start"<<endl;
try{ try{
TestRValue(); TestThreadPool();
}catch( std::exception e){ }catch( std::exception e){
std::cout<<"exception"<<e.what(); std::cout<<"exception"<<e.what();
} }

View File

@ -141,3 +141,22 @@ int TestRValue()
std::cout << "After move, str is \"" << str << "\"\n"; std::cout << "After move, str is \"" << str << "\"\n";
std::cout << "The contents of the vector are \"" << v[0]; std::cout << "The contents of the vector are \"" << v[0];
} }
class TestTask : public general::Task
{
public:
void Run()
{
std::cout<<"testwefwefwfwe"<<std::endl;
}
};
int TestThreadPool()
{
TestTask *t = new TestTask;
general::ThreadPool pool(10);
pool.Start();
pool.AddTask(t);
// pool.Stop();
getchar();
}

View File

@ -8,6 +8,7 @@
#include <chrono> #include <chrono>
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include "threadpool.h"
void TestPromiseFutureBefore(); void TestPromiseFutureBefore();
void TestThreadDetach(); void TestThreadDetach();
@ -15,3 +16,4 @@ void TestLockGuard();
int TestConditionVariable(); int TestConditionVariable();
int TestOptiomization(); int TestOptiomization();
int TestRValue(); int TestRValue();
int TestThreadPool();

View File

@ -5,9 +5,9 @@ namespace general{
CThreadPool::CThreadPool(int num) CThreadPool::CThreadPool(int num)
{ {
this->mThreadCnt = num; this->mThreadCnt = num;
mThreads.reserve(num); mStarted = false;
for(int i = 0;i < num;i++){ if (num < 2){
mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i)); mThreadCnt = 2;
} }
} }
@ -17,10 +17,16 @@ namespace general{
} }
std::unique_lock<std::mutex> lck(this->mMutex); std::unique_lock<std::mutex> lck(this->mMutex);
this->mTasks.push(t); this->mTasks.push(t);
mCd.notify_one();
} }
void CThreadPool::Start(){ 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(){ Task *CThreadPool::PopTask(){
@ -30,39 +36,66 @@ namespace general{
mCd.wait(lk); mCd.wait(lk);
} }
return this->mTasks.front(); Task *ret = this->mTasks.front();
this->mTasks.pop(); this->mTasks.pop();
return ret;
} }
void CThreadPool::Process(int id) void CThreadPool::Process(int id)
{ {
} std::cout << "thread id " << id << " started " << std::endl;
while (mStarted)
void CThreadPool::Stop(){
mStarted = false;
for (size_t i = 0; i != this->mThreads.size(); ++i)
{ {
if (mThreads[i]->joinable()) Task *task = PopTask();
std::cout<<"wake up "<<std::endl;
if (nullptr == task)
{ {
mThreads[i]->join(); continue;
}
else
{
task->Run();
} }
} }
} }
void CThreadPool::Stop()
{
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
mCd.notify_all();
}
for (auto &th : mThreads)
{
th->join();
}
mStarted = false;
}
bool CThreadPool::Started(){ bool CThreadPool::Started(){
return this->mStarted; return this->mStarted;
} }
CThreadPool::~CThreadPool(){ CThreadPool::~CThreadPool(){
if(this->mStarted){ std::cout << "desruction" << std::endl;
for (size_t i = 0; i != this->mThreads.size(); ++i) if (this->mStarted)
{ {
if (mThreads[i]->joinable())
{ {
mThreads[i]->join(); std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
mCd.notify_all();
}
for (size_t i = 0; i != this->mThreads.size(); ++i)
{
if (mThreads[i]->joinable())
{
mThreads[i]->join();
}
} }
} }
}
} }
} }

View File

@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <functional> #include <functional>
#include <utility> #include <utility>
#include "threadpool.h"
namespace general{ namespace general{
class CThreadPool; class CThreadPool;