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(){
std::cout<<"test start"<<endl;
try{
TestRValue();
TestThreadPool();
}catch( std::exception e){
std::cout<<"exception"<<e.what();
}

View File

@ -140,4 +140,23 @@ int TestRValue()
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
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,10 +8,12 @@
#include <chrono>
#include <cstdlib>
#include <vector>
#include "threadpool.h"
void TestPromiseFutureBefore();
void TestThreadDetach();
void TestLockGuard();
int TestConditionVariable();
int TestOptiomization();
int TestRValue();
int TestRValue();
int TestThreadPool();

View File

@ -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<std::mutex> 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<std::mutex> 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 "<<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(){
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<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 <functional>
#include <utility>
#include "threadpool.h"
namespace general{
class CThreadPool;