#pragma once #include #include #include #include #include #include #include #include #include namespace NYql { class TAsyncQueue: public std::enable_shared_from_this { public: using TWeakPtr = std::weak_ptr; using TPtr = std::shared_ptr; static TPtr Make(size_t numThreads, const TString& poolName); virtual ~TAsyncQueue() { MtpQueue_->Stop(); MtpQueue_.Destroy(); } template [[nodiscard]] static ::NThreading::TFuture<::NThreading::TFutureType<::TFunctionResult>> Async(const TWeakPtr& pool, TCallable&& func) { if (auto queue = pool.lock()) { return ::NThreading::Async(std::forward(func), *queue->MtpQueue_); } return ::NThreading::MakeErrorFuture<::NThreading::TFutureType<::TFunctionResult>>(std::make_exception_ptr(yexception() << "Thread pool is already stopped")); } private: TAsyncQueue(size_t numThreads, const TString& poolName); THolder MtpQueue_; }; } // namespace NYql