From 6e877a3bfed4d1b4406288458bf91d8a5b943383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=98=E4=B8=8D=E5=A6=82=E4=B8=80=E5=8F=AA=E7=8C=AA?= =?UTF-8?q?=E5=A8=81=E6=AD=A6?= Date: Tue, 27 Feb 2024 11:53:49 +0800 Subject: [PATCH] =?UTF-8?q?future=E3=80=81=20promise=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/sync_queue.h | 4 +-- src/base/thread_pool.h | 2 +- src/main.cpp | 75 +++++++++++++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/base/sync_queue.h b/src/base/sync_queue.h index 9e28f8a..8a48d84 100644 --- a/src/base/sync_queue.h +++ b/src/base/sync_queue.h @@ -20,7 +20,8 @@ private: public: explicit SyncQueue(int maxSize) - : mMaxSize(maxSize) { + : mMaxSize(maxSize), + mStop(false) { } /** @@ -43,7 +44,6 @@ public: if (mStop) return; mQueue.push_back(std::forward(f)); mNotEmpty.notify_all(); - mNotEmpty.notify_all(); } diff --git a/src/base/thread_pool.h b/src/base/thread_pool.h index 3be56f1..9e3f9fb 100644 --- a/src/base/thread_pool.h +++ b/src/base/thread_pool.h @@ -60,7 +60,7 @@ private: } mThreadGroup.clear(); } - + public: ThreadPool(int numThreads = std::thread::hardware_concurrency(), int maxTask = 1000) : mQueue(maxTask) { diff --git a/src/main.cpp b/src/main.cpp index aafcb3f..dbc804b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include std::mutex g_lock; @@ -256,25 +257,69 @@ void message_test() { bus.SendReq(i); bus.SendReq(2); bus.SendReq(2); +} + +#include + +void async_test() { + // promise 保有的是一个共享状态的值 + std::promise pr; + std::thread t([](std::promise &p) { + p.set_value_at_thread_exit(9); + }, std::ref(pr)); + std::future f = pr.get_future(); + auto res = f.get(); + std::cout << "res : " << res << std::endl; + + // packaged_task 保存的是一个函数 + std::packaged_task task([](int a) { + return a + 7; + }); + std::thread t1(std::ref(task), 1); + std::future f1 = task.get_future(); + auto r = f1.get(); + std::cout << "r : " << r << std::endl; + std::packaged_task task1([](int a) { + return a + 7; + }); + auto f2 = task1.get_future(); + task1(3); + std::cout << "r2 : " << f2.get() << std::endl; + + // std::launch::async 该种模式下立即创建线程 + std::future func1 = std::async(std::launch::async, []() { + return 8; + }); + + std::cout << "func1 : " << func1.get() << std::endl; + + std::future func2 = std::async(std::launch::deferred, []() { + return 8; + }); + + std::cout << "func2 : " << func2.get() << std::endl; } + int main() { -// 线程测试 - thread_test(); -// 同步队列测试 +//// 线程测试 +// thread_test(); +//// 同步队列测试 sync_test(); -// 时间工具测试 - chrono_test(); -// 线程池测试 - threadpool_test(); -// AOP测试 - aop_test(); -// Any测试 - any_test(); -// IoC测试 - ioc_test(); -// 总线测试 - message_test(); +//// 时间工具测试 +// chrono_test(); +//// 线程池测试 +// threadpool_test(); +//// AOP测试 +// aop_test(); +//// Any测试 +// any_test(); +//// IoC测试 +// ioc_test(); +//// 总线测试 +// message_test(); +// 异步测试 +// async_test(); return 0; } \ No newline at end of file