diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/threading/future/async.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/threading/future/async.h')
-rw-r--r-- | library/cpp/threading/future/async.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/library/cpp/threading/future/async.h b/library/cpp/threading/future/async.h new file mode 100644 index 0000000000..8543fdd5c6 --- /dev/null +++ b/library/cpp/threading/future/async.h @@ -0,0 +1,31 @@ +#pragma once + +#include "future.h" + +#include <util/generic/function.h> +#include <util/thread/pool.h> + +namespace NThreading { + /** + * @brief Asynchronously executes @arg func in @arg queue returning a future for the result. + * + * @arg func should be a callable object with signature T(). + * @arg queue where @arg will be executed + * @returns For @arg func with signature T() the function returns TFuture<T> unless T is TFuture<U>. + * In this case the function returns TFuture<U>. + * + * If you want to use another queue for execution just write an overload, @see ExtensionExample + * unittest. + */ + template <typename Func> + TFuture<TFutureType<TFunctionResult<Func>>> Async(Func&& func, IThreadPool& queue) { + auto promise = NewPromise<TFutureType<TFunctionResult<Func>>>(); + auto lambda = [promise, func = std::forward<Func>(func)]() mutable { + NImpl::SetValue(promise, func); + }; + queue.SafeAddFunc(std::move(lambda)); + + return promise.GetFuture(); + } + +} |