AlexaClientSDK  1.26.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 #ifdef THREAD_AFFINITY
22 #include <map>
23 #endif
24 #include <string>
25 
26 #include "WorkerThread.h"
27 
28 namespace alexaClientSDK {
29 namespace avsCommon {
30 namespace utils {
31 namespace threading {
32 
33 static const size_t DEFAULT_MAX_THREAD_POOL_THREADS = 20;
34 
45 class ThreadPool {
46 public:
50  ThreadPool(size_t maxThreads = DEFAULT_MAX_THREAD_POOL_THREADS);
51 
55  ~ThreadPool();
56 
63  std::unique_ptr<WorkerThread> obtainWorker(std::string optionalMoniker = "");
64 
70  void releaseWorker(std::unique_ptr<WorkerThread> workerThread);
71 
78  void setMaxThreads(size_t maxThreads);
79 
84  uint32_t getMaxThreads();
85 
93  void getStats(
94  uint64_t& threadsCreated,
95  uint64_t& threadsObtained,
96  uint64_t& threadsReleasedToPool,
97  uint64_t& threadsReleasedFromPool);
98 
103  static std::shared_ptr<ThreadPool> getDefaultThreadPool();
104 
105 private:
106 #ifdef THREAD_AFFINITY
107  std::map<std::string, std::list<std::unique_ptr<WorkerThread>>::iterator> m_workerMap;
109 #endif
110  std::list<std::unique_ptr<WorkerThread>> m_workerQueue;
112 
114  size_t m_maxPoolThreads;
115 
117  uint64_t m_created;
118 
120  uint64_t m_obtained;
121 
123  uint64_t m_releasedToPool;
124 
126  uint64_t m_releasedFromPool;
127 
129  std::mutex m_queueMutex;
130 };
131 
132 } // namespace threading
133 } // namespace utils
134 } // namespace avsCommon
135 } // namespace alexaClientSDK
136 
137 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_THREADPOOL_H_
ThreadPool(size_t maxThreads=DEFAULT_MAX_THREAD_POOL_THREADS)
Definition: ThreadPool.cpp:44
void getStats(uint64_t &threadsCreated, uint64_t &threadsObtained, uint64_t &threadsReleasedToPool, uint64_t &threadsReleasedFromPool)
Definition: ThreadPool.cpp:123
static std::shared_ptr< ThreadPool > getDefaultThreadPool()
Definition: ThreadPool.cpp:135
void setMaxThreads(size_t maxThreads)
Definition: ThreadPool.cpp:110
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
uint32_t getMaxThreads()
Definition: ThreadPool.cpp:118
std::unique_ptr< WorkerThread > obtainWorker(std::string optionalMoniker="")
Definition: ThreadPool.cpp:58
void releaseWorker(std::unique_ptr< WorkerThread > workerThread)
Definition: ThreadPool.cpp:92
static const size_t DEFAULT_MAX_THREAD_POOL_THREADS
Definition: ThreadPool.h:33

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