no message

master
caiyuzheng 2021-03-12 00:38:18 +08:00
parent b893e33b4c
commit 1ed0fe6c1f
4 changed files with 25 additions and 15 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.12)
project(cpp11)
message("current dir" ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -71,6 +71,7 @@ void TestLockGuard(){
std::mutex cMtx; // 全局互斥锁.
std::condition_variable gCv; // 全局条件变量.
bool gReady = false; // 全局标志位.
void do_print_id(int id){
std::unique_lock <std::mutex> lck(cMtx);
while (!gReady)
@ -97,4 +98,4 @@ int TestConditionVariable() {
for (auto & th:threads)
th.join();
return 0;
}
}

View File

@ -1,8 +1,8 @@
#include "threadpool.h"
// 参考于https://www.cnblogs.com/bigosprite/p/11071462.html
namespace general{
void work(general::CThreadPool *pool){
void work(general::CThreadPool *pool)
{
}
CThreadPool::CThreadPool(int num)
{
@ -21,24 +21,32 @@ namespace general{
}
void CThreadPool::Start(){
}
Task *CThreadPool::PopTask(){
if(!this->mTasks.empty()){
return this->mTasks.front();
this->mTasks.pop();
std::unique_lock<std::mutex> lk(this->mMutex);
while (mTasks.empty() && mStarted)
{
mCd.wait(lk);
}
return this->mTasks.front();
this->mTasks.pop();
}
void CThreadPool::Process(int id)
{
}
void CThreadPool::Stop(){
mStarted = false;
for (size_t i = 0; i != this->mThreads.size(); ++i)
{
// if (mThreads[i].joinable())
// {
// mThreads[i].join();
// }
if (mThreads[i]->joinable())
{
mThreads[i]->join();
}
}
}

View File

@ -57,12 +57,13 @@ namespace general{
void Start();
void Stop();
bool Started();
Task *PopTask();
private:
CThreadPool();
void work(void*);
void Process(int id);
uint32_t mThreadCnt;
bool mStarted;
Task *PopTask();
std::vector<std::thread*> mThreads;
std::queue<Task *> mTasks;
std::mutex mMutex;