no message

master
caiyuzheng 2021-05-03 02:19:14 +08:00
parent 662860689d
commit 4f742a96df
4 changed files with 19 additions and 13 deletions

View File

@ -10,5 +10,4 @@ link_directories("./third/jsoncpp/lib/")
link_libraries(jsoncpp) link_libraries(jsoncpp)
add_executable(cpp11 ${SOURCE} ) add_executable(cpp11 ${SOURCE} )
include_directories("./third/jsoncpp/include/pkgsrc/include/json") include_directories("./third/jsoncpp/include/pkgsrc/include/json")
# target_link_libraries(cpp11 "jsoncpp")

View File

@ -4,13 +4,12 @@
#include<iostream> #include<iostream>
using namespace std; using namespace std;
#include<stdio.h>
#include "thread_usage.h" #include "thread_usage.h"
#include "threadpool.h" #include "threadpool.h"
#include "json.h" #include "json.h"
extern "C"{ extern "C"{
#include<stdio.h>
#include <time.h> #include <time.h>
} }

View File

@ -148,10 +148,9 @@ class TestTask : public general::Task
public: public:
void Run() void Run()
{ {
for (int i = 0;i < 100; i ++){ auto t1 = std::chrono::steady_clock::now();
std::string strValue = "{\"key\":\"value1\",\ std::string strValue = "{\"key\":\"value1\",\
\"array\":[{\"arraykey\":1},{\"arraykey\":2}]}"; \"array\":[{\"arraykey\":1},{\"arraykey\":2}]}";
Json::Reader reader; Json::Reader reader;
Json::Reader reader1; Json::Reader reader1;
Json::Reader reader2; Json::Reader reader2;
@ -160,10 +159,15 @@ public:
Json::Value root1; Json::Value root1;
Json::Value root2; Json::Value root2;
for (int i = 0;i < ; i ++){
reader.parse(strValue, root); reader.parse(strValue, root);
reader1.parse(strValue, root1); reader1.parse(strValue, root1);
reader2.parse(strValue, root2); reader2.parse(strValue, root2);
} }
auto t2 = std::chrono::steady_clock::now();
double dr_ms = std::chrono::duration<double,std::milli>(t2-t1).count();
std::cout<<"expired is "<<dr_ms<<std::endl;
} }
}; };
// 42973.6 // 42973.6
@ -172,12 +176,17 @@ int TestThreadPool()
{ {
//run code //run code
TestTask *t = new TestTask; TestTask *t = new TestTask;
general::ThreadPool pool(1); general::ThreadPool pool(3);
pool.Start();
auto t1 = std::chrono::steady_clock::now(); auto t1 = std::chrono::steady_clock::now();
for(int i = 0;i < 2000;i++){ for(int i = 0;i < 10;i++){
pool.AddTask(t); pool.AddTask(t);
} }
pool.Start();
for(int i = 0;i < 20;i++){
pool.AddTask(t);
}
pool.StopAll(); pool.StopAll();
auto t2 = std::chrono::steady_clock::now(); auto t2 = std::chrono::steady_clock::now();
double dr_ms = std::chrono::duration<double,std::milli>(t2-t1).count(); double dr_ms = std::chrono::duration<double,std::milli>(t2-t1).count();

View File

@ -54,7 +54,6 @@ namespace general{
{ {
mQueueAvaliableCondition.wait(lk); mQueueAvaliableCondition.wait(lk);
} }
if(!mStarted){ if(!mStarted){
return nullptr; return nullptr;
} }
@ -97,8 +96,8 @@ namespace general{
void CThreadPool::StopAll() void CThreadPool::StopAll()
{ {
{ {
while(this->mTasks.size() > 0) ; while(this->mTasks.size() > 0)
std::lock_guard<std::mutex> lk(mMutex); std::this_thread::sleep_for(std::chrono::milliseconds(100));
mStarted = false; mStarted = false;
mQueueAvaliableCondition.notify_all(); mQueueAvaliableCondition.notify_all();
} }