From 5d7cbec34cd5e473d5768b39054d99bc72ddad62 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 27 Apr 2023 12:55:30 +1000 Subject: Move DB interactions to a background thread --- src/database/include/db_task.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/database/include/db_task.hpp (limited to 'src/database/include/db_task.hpp') diff --git a/src/database/include/db_task.hpp b/src/database/include/db_task.hpp new file mode 100644 index 00000000..39f932b0 --- /dev/null +++ b/src/database/include/db_task.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +namespace database { + +auto StartDbTask() -> bool; +auto QuitDbTask() -> void; + +auto SendToDbTask(std::function fn) -> void; + +template +auto RunOnDbTask(std::function fn) -> std::future { + std::shared_ptr> promise = + std::make_shared>(); + SendToDbTask([=]() { promise->set_value(std::invoke(fn)); }); + return promise->get_future(); +} + +template <> +auto RunOnDbTask(std::function fn) -> std::future; + +} // namespace database -- cgit v1.2.3