AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
ThreadPool.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_THREADPOOL_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_THREADPOOL_H_
18 
19 #include <condition_variable>
20 #include <list>
21 #include <string>
22 
23 #include "WorkerThread.h"
24 
25 namespace alexaClientSDK {
26 namespace avsCommon {
27 namespace utils {
28 namespace threading {
29 
30 static const size_t DEFAULT_MAX_THREAD_POOL_THREADS = 20;
31 
42 class ThreadPool {
43 public:
47  ThreadPool(size_t maxThreads = DEFAULT_MAX_THREAD_POOL_THREADS);
48 
52  ~ThreadPool();
53 
62  std::unique_ptr<WorkerThread> obtainWorker();
63 
69  void releaseWorker(std::unique_ptr<WorkerThread> workerThread);
70 
77  void setMaxThreads(size_t maxThreads);
78 
83  size_t getMaxThreads();
84 
92  void getStats(
93  uint64_t& threadsCreated,
94  uint64_t& threadsObtained,
95  uint64_t& threadsReleasedToPool,
96  uint64_t& threadsReleasedFromPool);
97 
102  static std::shared_ptr<ThreadPool> getDefaultThreadPool();
103 
104 private:
106  std::list<std::unique_ptr<WorkerThread>> m_workerQueue;
107 
109  size_t m_maxPoolThreads;
110 
112  uint64_t m_created;
113 
115  uint64_t m_obtained;
116 
118  uint64_t m_releasedToPool;
119 
121  uint64_t m_releasedFromPool;
122 
124  std::mutex m_queueMutex;
125 };
126 
127 } // namespace threading
128 } // namespace utils
129 } // namespace avsCommon
130 } // namespace alexaClientSDK
131 
132 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_THREADPOOL_H_
ThreadPool(size_t maxThreads=DEFAULT_MAX_THREAD_POOL_THREADS)
std::unique_ptr< WorkerThread > obtainWorker()
void getStats(uint64_t &threadsCreated, uint64_t &threadsObtained, uint64_t &threadsReleasedToPool, uint64_t &threadsReleasedFromPool)
static std::shared_ptr< ThreadPool > getDefaultThreadPool()
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void releaseWorker(std::unique_ptr< WorkerThread > workerThread)
static const size_t DEFAULT_MAX_THREAD_POOL_THREADS
Definition: ThreadPool.h:30

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0