Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Async.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#pragma once
21
23
24namespace sese {
25
30template<class RETURN_TYPE>
31std::shared_future<RETURN_TYPE> async(const std::function<RETURN_TYPE()> &task) noexcept;
32
38template<class RETURN_TYPE>
39std::shared_future<RETURN_TYPE> async(ThreadPool &pool, const std::function<RETURN_TYPE()> &task) noexcept;
40
45template<class RETURN_TYPE>
46std::shared_future<RETURN_TYPE> asyncWithGlobalPool(const std::function<RETURN_TYPE()> &task) noexcept;
47
48} // namespace sese
49
50template<class RETURN_TYPE>
51std::shared_future<RETURN_TYPE> sese::async(const std::function<RETURN_TYPE()> &task) noexcept {
52 std::packaged_task<RETURN_TYPE()> packaged_task(task);
53 std::shared_future<RETURN_TYPE> future(packaged_task.get_future());
54
55 std::thread(
56 [&](std::packaged_task<RETURN_TYPE()> task) {
57 task();
58 },
59 std::move(packaged_task)
60 )
61 .detach();
62
63 return future;
64}
65
66template<class RETURN_TYPE>
67std::shared_future<RETURN_TYPE> sese::async(ThreadPool &pool, const std::function<RETURN_TYPE()> &task) noexcept {
68 return pool.postTask<RETURN_TYPE>(task);
69}
70
71template<class RETURN_TYPE>
72std::shared_future<RETURN_TYPE> sese::asyncWithGlobalPool(const std::function<RETURN_TYPE()> &task) noexcept {
74}