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,23 +36,42 @@ 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)
{
Task *task = PopTask();
std::cout<<"wake up "<<std::endl;
if (nullptr == task)
{
continue;
}
else
{
task->Run();
}
}
} }
void CThreadPool::Stop(){ void CThreadPool::Stop()
{
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false; mStarted = false;
for (size_t i = 0; i != this->mThreads.size(); ++i) mCd.notify_all();
{
if (mThreads[i]->joinable())
{
mThreads[i]->join();
} }
for (auto &th : mThreads)
{
th->join();
} }
mStarted = false;
} }
bool CThreadPool::Started(){ bool CThreadPool::Started(){
@ -54,7 +79,15 @@ namespace general{
} }
CThreadPool::~CThreadPool(){ CThreadPool::~CThreadPool(){
if(this->mStarted){ std::cout << "desruction" << std::endl;
if (this->mStarted)
{
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
mCd.notify_all();
}
for (size_t i = 0; i != this->mThreads.size(); ++i) for (size_t i = 0; i != this->mThreads.size(); ++i)
{ {
if (mThreads[i]->joinable()) if (mThreads[i]->joinable())

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;