diff --git a/test/src/cpp11/CMakeLists.txt b/test/src/cpp11/CMakeLists.txt index b1a6fb4..f1ca2cf 100644 --- a/test/src/cpp11/CMakeLists.txt +++ b/test/src/cpp11/CMakeLists.txt @@ -4,10 +4,11 @@ add_definitions(-std=c++11) set(CMAKE_BUILD_TYPE DEBUG) -set(CMAKE_CXX_FLAGS " /MTd ") +set(CMAKE_CXX_FLAGS " /MDd ") set(CMAKE_CXX_FLAGS_DEBUG "/DEBUG") set(CMAKE_CXX_FLAGS_RELEASE "") + message("current dir" ${CMAKE_CURRENT_SOURCE_DIR}) message(info ${SOURCE}) link_directories("./third/jsoncpp/lib/") @@ -16,11 +17,13 @@ link_directories("./third/gtest/lib") link_libraries(generallib) link_libraries(gtestd) +link_libraries(jsoncpp) add_executable(cpp11 cpp11_test.cpp ) add_executable(gtest gtest.cpp ) +add_executable(thread_test thread_usage.cpp threadpool.cpp) -include_directories("./third/jsoncpp/include/pkgsrc/include/json") +include_directories("./third/jsoncpp/include/json") include_directories("../../../obj/inc/") include_directories("./third/gtest/include") diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index 34334e3..36270cf 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-03-15 23:07:25 - * @LastEditTime: 2021-11-18 11:14:15 + * @LastEditTime: 2021-11-23 11:29:15 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \cpp11\cpp11_test.cpp @@ -43,7 +43,7 @@ int main(int argc, char **argv) }); auto x = ip.Front(); std::cout<<*x<<"\r\n"; - + std::cout<& promiseObj) { std::cout<<"end"< promiseObj; std::future futureObj = promiseObj.get_future(); std::thread t(&thread_set_promise, std::ref(promiseObj)); @@ -21,8 +21,7 @@ void TestPromiseFutureBefore(){ t.join(); } -void independentThread() -{ +void independentThread() { std::cout << "Starting concurrent thread.\n"; std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "Exiting concurrent thread.\n"; @@ -56,7 +55,7 @@ void print_thread_id (int id) { } } -void TestLockGuard(){ +void TestLockGuard() { std::thread threads[10]; // spawn 10 threads: for (int i=0; i<10; ++i) @@ -71,7 +70,7 @@ std::mutex cMtx; // 全局互斥锁. std::condition_variable gCv; // 全局条件变量. bool gReady = false; // 全局标志位. -void do_print_id(int id){ +void do_print_id(int id) { std::unique_lock lck(cMtx); while (!gReady) gCv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后, @@ -82,7 +81,6 @@ void do_print_id(int id){ void go(){ gReady = true; gCv.notify_all(); // 唤醒所有线程. - } int TestConditionVariable() { @@ -91,16 +89,14 @@ int TestConditionVariable() { for (int i = 0; i < 10; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; - go(); - + for (auto & th:threads) th.join(); return 0; } -class TestClass -{ +class TestClass { public: TestClass() : num(new int(0)) { @@ -119,17 +115,18 @@ public: private: int *num; }; -TestClass get_demo() -{ + +TestClass get_demo() { return TestClass(); } -int TestOptiomization() -{ + +int TestOptiomization() { TestClass a = get_demo(); return 0; } -int TestRValue() -{ + + +int TestRValue() { std::string str = "Hello"; std::vector v; //调用常规的拷贝构造函数,新建字符数组,拷贝数据 @@ -143,8 +140,7 @@ int TestRValue() } int global = 0; -class TestTask : public general::Task -{ +class TestTask : public general::Task { public: void Run() { @@ -163,17 +159,22 @@ public: reader.parse(strValue, root); reader1.parse(strValue, root1); reader2.parse(strValue, root2); - } auto t2 = std::chrono::steady_clock::now(); double dr_ms = std::chrono::duration(t2-t1).count(); std::cout<<"expired is "< a{0}; +atomic b{0}; +void ValueSet(int) +{ + a.store(0,memory_order_relaxed); + b.store(0,memory_order_relaxed); + + for(int i = 0;i < 100;i++){ + int t=1; + a.store(t,memory_order_relaxed); + b.store(2,memory_order_relaxed); + } +} +void Observer(int) +{ + while(b.load(memory_order_relaxed)!=2);//自旋等待 + cout< t1) { + t1(1); +} + +int TestFuntionParadim() { + + return 0; +} + + +// 一个异步线程的封装 +template +class ASyncProcess { + public: + ASyncProcess(T t){ + std::cout<<"ASyncProcess construct"<Run(); + this->Done(t); + }); + } + bool Finish(){ + return m_finish; + } + virtual void Run() { + std::cout<<"ASyncProcess::Run()"<detach(); + } + private: + bool m_finish; + std::thread *mThread; +}; + +class TestProcess : public ASyncProcess{ +public: + TestProcess(int x ) + :ASyncProcess(x) + { + + } + void Run(){ + Sleep(5000); + std::cout<<"ASyncProcess\r\n"; + } + void Done(int i){ + std::cout<<"Done\r\n"; + i = 4; + } +}; + +int main() +{ + TestFuntionParadim(); + int j =111; + int &x = j; + TestProcess p1(x); + getchar(); + std::cout< @@ -9,7 +17,19 @@ #include #include #include "threadpool.h" -#include "third\jsoncpp\include\pkgsrc\include\json\json.h" +#include "json.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; void TestPromiseFutureBefore(); void TestThreadDetach(); @@ -17,4 +37,5 @@ void TestLockGuard(); int TestConditionVariable(); int TestOptiomization(); int TestRValue(); -int TestThreadPool(); \ No newline at end of file +int TestThreadPool(); +int TestFuntionParadim(); \ No newline at end of file diff --git a/test/src/cpp11/threadpool.cpp b/test/src/cpp11/threadpool.cpp index 724a57b..5caab14 100644 --- a/test/src/cpp11/threadpool.cpp +++ b/test/src/cpp11/threadpool.cpp @@ -16,14 +16,13 @@ unsigned int CoreCount() namespace general{ - CThreadPool::CThreadPool(int num) - { + CThreadPool::CThreadPool(int num) { this->mThreadCnt = num; mStarted = false; } - int CThreadPool::AddTask(Task *t){ + int CThreadPool::AddTask(Task *t) { if ( nullptr == t){ return -1; } @@ -39,7 +38,7 @@ namespace general{ } } - void CThreadPool::Start(){ + void CThreadPool::Start() { mStarted = true; mThreads.reserve(this->mThreadCnt); for (int i = 0; i < mThreadCnt; i++) @@ -48,7 +47,7 @@ namespace general{ } } - Task *CThreadPool::PopTask(){ + Task *CThreadPool::PopTask() { std::unique_lock lk(this->mMutex); while (mTasks.empty() && mStarted) { @@ -62,8 +61,7 @@ namespace general{ return ret; } - void CThreadPool::Process(int id) - { + void CThreadPool::Process(int id) { // std::cout << "thread id " << id << " started " << std::endl; while (mStarted) { @@ -75,12 +73,12 @@ namespace general{ else { task->Run(); + task->OnDone(); } } } - void CThreadPool::Stop() - { + void CThreadPool::Stop() { { std::lock_guard lk(mMutex); mStarted = false; @@ -93,8 +91,7 @@ namespace general{ } mStarted = false; } - void CThreadPool::StopAll() - { + void CThreadPool::StopAll() { { while(this->mTasks.size() > 0) std::this_thread::sleep_for(std::chrono::milliseconds(100)); diff --git a/test/src/cpp11/threadpool.h b/test/src/cpp11/threadpool.h index cd97f39..1af6dba 100644 --- a/test/src/cpp11/threadpool.h +++ b/test/src/cpp11/threadpool.h @@ -1,3 +1,11 @@ +/* + * @Author: your name + * @Date: 2021-10-09 10:03:45 + * @LastEditTime: 2021-11-25 15:26:02 + * @LastEditors: Please set LastEditors + * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE + * @FilePath: \cpp11\threadpool.h + */ #pragma once #include #include @@ -22,13 +30,13 @@ extern "C"{ } namespace general{ class CThreadPool; - enum PRIORITY - { + enum PRIORITY { MIN = 1, NORMAL = 25, MAX = 50 }; - typedef class CTask{ + + class Task{ public: - CTask() { + Task() { } void SetPriority(int priority) { if (priority>(PRIORITY::MAX)) @@ -39,11 +47,12 @@ namespace general{ { priority = (PRIORITY::MIN); } - } + } virtual void Run() = 0; + virtual void OnDone() = 0; protected: int priority_; - }Task; + }; class Worker{ public: @@ -82,4 +91,5 @@ namespace general{ std::mutex mMutex; std::condition_variable mQueueAvaliableCondition; }ThreadPool; -} \ No newline at end of file +} +