From e5b22514c797a23b52339785e0625f054d6969ec Mon Sep 17 00:00:00 2001 From: Lord-Turmoil Date: Tue, 3 Dec 2024 10:56:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfix=20task=20not=20started=20asynch?= =?UTF-8?q?ronously=20=F0=9F=8F=B7=EF=B8=8Fupdate=20version=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- src/common/Base.cpp | 2 +- src/threading/Task.h | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index feefa0a..f62614b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(BINARY_NAME minetcore) set(CMAKE_CXX_STANDARD 17) project(${PROJECT_NAME} - VERSION 1.0.1 + VERSION 1.0.2 DESCRIPTION "A mini HTTP server library with C++" LANGUAGES CXX) diff --git a/src/common/Base.cpp b/src/common/Base.cpp index b7943e4..6a7e764 100644 --- a/src/common/Base.cpp +++ b/src/common/Base.cpp @@ -2,6 +2,6 @@ MINET_BEGIN -const char* Version = "0.1.0"; +const char* Version = "1.0.2"; MINET_END diff --git a/src/threading/Task.h b/src/threading/Task.h index 7887f55..81339f6 100644 --- a/src/threading/Task.h +++ b/src/threading/Task.h @@ -47,6 +47,7 @@ class Task final : public std::enable_shared_from_this private: std::packaged_task _task; std::future _future; + std::thread _thread; }; /** @@ -79,6 +80,7 @@ template class ValueTask final : public std::enable_shared_fr private: std::packaged_task _task; std::future _future; + std::thread _thread; }; /* @@ -116,8 +118,12 @@ inline Ref Task::Completed() inline Ref Task::StartAsync() { + if (_future.valid()) + { + throw std::runtime_error("Task already started"); + } _future = _task.get_future(); - _task(); + _thread = std::thread(std::move(_task)); return shared_from_this(); } @@ -127,6 +133,7 @@ inline void Task::Await() { throw std::runtime_error("Task not started"); } + _thread.join(); _future.get(); } @@ -175,8 +182,12 @@ template Ref> ValueTask::Complete template Ref> ValueTask::StartAsync() { + if (_future.valid()) + { + throw std::runtime_error("Task already started"); + } _future = _task.get_future(); - _task(); + _thread = std::thread(std::move(_task)); return this->shared_from_this(); } @@ -186,6 +197,7 @@ template TResult ValueTask::Await() { throw std::runtime_error("Task not started"); } + _thread.join(); return _future.get(); }