From 194389ef6da87818f540be62af73fe0296c49a24 Mon Sep 17 00:00:00 2001 From: Stepan Date: Thu, 6 Aug 2020 13:23:49 +0300 Subject: [PATCH] Refactor templates & submit doc --- lib/main/thread/piblockingdequeue.h | 4 ++-- lib/main/thread/piexecutor.h | 24 ++++++++++++++++++------ tests/concurrent/ExecutorUnitTest.cpp | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/main/thread/piblockingdequeue.h b/lib/main/thread/piblockingdequeue.h index 2ca96bbd..844755bd 100644 --- a/lib/main/thread/piblockingdequeue.h +++ b/lib/main/thread/piblockingdequeue.h @@ -28,10 +28,10 @@ * @brief A Queue that supports operations that wait for the queue to become non-empty when retrieving an element, and * wait for space to become available in the queue when storing an element. */ -template +template class Queue_ = std::deque > class PIBlockingDequeue { public: - typedef typename std::deque QueueType; + typedef Queue_ QueueType; /** * @brief Constructor diff --git a/lib/main/thread/piexecutor.h b/lib/main/thread/piexecutor.h index 3fb31f37..a402c3ca 100644 --- a/lib/main/thread/piexecutor.h +++ b/lib/main/thread/piexecutor.h @@ -62,7 +62,7 @@ public: FunctionWrapper& operator=(const FunctionWrapper&) = delete; }; -template class Dequeue_> +template > class PIThreadPoolExecutorTemplate { public: NO_COPY_CLASS(PIThreadPoolExecutorTemplate) @@ -121,7 +121,7 @@ public: protected: std::atomic_bool isShutdown_; - Dequeue_ taskQueue; + Dequeue_ taskQueue; PIVector threadPool; template @@ -145,15 +145,13 @@ protected: } }; -typedef PIThreadPoolExecutorTemplate PIThreadPoolExecutor; +typedef PIThreadPoolExecutorTemplate<> PIThreadPoolExecutor; #ifdef DOXYGEN /** * @brief Thread pools address two different problems: they usually provide improved performance when executing large * numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and * managing the resources, including threads, consumed when executing a collection of tasks. - * - * TODO adapt documentation to template */ class PIThreadPoolExecutor { public: @@ -161,14 +159,28 @@ public: virtual ~PIThreadPoolExecutor(); + /** + * @brief Submits a Runnable task for execution and returns a Future representing that task. The Future's get method + * will return null upon successful completion. + * + * @tparam FunctionType - custom type of function with operator() and return type + * @tparam R - derived from FunctionType return type + * + * @param callable - the task to submit + * @return a future representing pending completion of the task + */ + std::future submit(FunctionType&& callable); + /** * @brief Executes the given task sometime in the future. The task execute in an existing pooled thread. If the task * cannot be submitted for execution, either because this executor has been shutdown or because its capacity has been * reached. * + * @tparam FunctionType - custom type of function with operator() and return type + * * @param runnable not empty function for thread pool execution */ - void execute(const std::function & runnable); + void execute(FunctionType&& runnable); /** * @brief Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be diff --git a/tests/concurrent/ExecutorUnitTest.cpp b/tests/concurrent/ExecutorUnitTest.cpp index e4c1eed3..41470300 100644 --- a/tests/concurrent/ExecutorUnitTest.cpp +++ b/tests/concurrent/ExecutorUnitTest.cpp @@ -48,7 +48,7 @@ public: MOCK_METHOD0(remainingCapacity, size_t()); }; -typedef PIThreadPoolExecutorTemplate, MockDeque> PIThreadPoolExecutorMoc_t; +typedef PIThreadPoolExecutorTemplate, MockDeque> PIThreadPoolExecutorMoc_t; class PIThreadPoolExecutorMoc : public PIThreadPoolExecutorMoc_t { public: