Skip to content

Commit

Permalink
🐛fix task not started asynchronously
Browse files Browse the repository at this point in the history
🏷️update version string
  • Loading branch information
Lord-Turmoil committed Dec 3, 2024
1 parent 59d5397 commit e5b2251
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/common/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

MINET_BEGIN

const char* Version = "0.1.0";
const char* Version = "1.0.2";

MINET_END
16 changes: 14 additions & 2 deletions src/threading/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Task final : public std::enable_shared_from_this<Task>
private:
std::packaged_task<void()> _task;
std::future<void> _future;
std::thread _thread;
};

/**
Expand Down Expand Up @@ -79,6 +80,7 @@ template <typename TResult> class ValueTask final : public std::enable_shared_fr
private:
std::packaged_task<TResult()> _task;
std::future<TResult> _future;
std::thread _thread;
};

/*
Expand Down Expand Up @@ -116,8 +118,12 @@ inline Ref<Task> Task::Completed()

inline Ref<Task> 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();
}

Expand All @@ -127,6 +133,7 @@ inline void Task::Await()
{
throw std::runtime_error("Task not started");
}
_thread.join();
_future.get();
}

Expand Down Expand Up @@ -175,8 +182,12 @@ template <typename TResult> Ref<ValueTask<TResult>> ValueTask<TResult>::Complete

template <typename TResult> Ref<ValueTask<TResult>> ValueTask<TResult>::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();
}

Expand All @@ -186,6 +197,7 @@ template <typename TResult> TResult ValueTask<TResult>::Await()
{
throw std::runtime_error("Task not started");
}
_thread.join();
return _future.get();
}

Expand Down

0 comments on commit e5b2251

Please sign in to comment.