![]() |
AlexaClientSDK
3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
|
#include <ThreadPool.h>
Public Member Functions | |
ThreadPool (size_t maxThreads=DEFAULT_MAX_THREAD_POOL_THREADS) | |
~ThreadPool () | |
std::unique_ptr< WorkerThread > | obtainWorker () |
void | releaseWorker (std::unique_ptr< WorkerThread > workerThread) |
void | setMaxThreads (size_t maxThreads) |
size_t | getMaxThreads () |
void | getStats (uint64_t &threadsCreated, uint64_t &threadsObtained, uint64_t &threadsReleasedToPool, uint64_t &threadsReleasedFromPool) |
Static Public Member Functions | |
static std::shared_ptr< ThreadPool > | getDefaultThreadPool () |
The ThreadPool
holds a pool of worker threads that each represent an OS thread. The pool begins empty. When ThreadPool::obtainWorker
is called when the pool is empty, a new worker thread will be created and returned to the caller; otherwise, when the pool is not empty, one of the existing worker threads will be removed from the pool and returned to the caller. The ThreadPool
holds a configurable maximum number of worker threads. Threads are released back into the pool when ThreadPool:releaseWorker
is called. Once the maximum worker threads are present in the pool, releasing a worker thread will cause one of the existing pool's worker threads to be destructed and released so that the pool does not grow beyond the configured maximum. The ThreadPool also maintains and provides statistics for the number of threads threads created, obtained, released into the pool, and released from the pool.
alexaClientSDK::avsCommon::utils::threading::ThreadPool::ThreadPool | ( | size_t | maxThreads = DEFAULT_MAX_THREAD_POOL_THREADS | ) |
Constructs a thread pool that keeps track of the threads it maintains by moniker.
alexaClientSDK::avsCommon::utils::threading::ThreadPool::~ThreadPool | ( | ) |
Destructs the thread pool.
|
static |
Obtain a shared pointer to the default singleton thread pool.
size_t alexaClientSDK::avsCommon::utils::threading::ThreadPool::getMaxThreads | ( | ) |
Obtain the current maximum threads for the thread pool.
void alexaClientSDK::avsCommon::utils::threading::ThreadPool::getStats | ( | uint64_t & | threadsCreated, |
uint64_t & | threadsObtained, | ||
uint64_t & | threadsReleasedToPool, | ||
uint64_t & | threadsReleasedFromPool | ||
) |
Obtain statics for the thread pool
threadsCreated | the total number of threads the pool has created. |
threadsObtained | the number of threads obtained from the pool. |
threadsReleasedToPool | the number of threads released from the caller into the pool. |
threadsReleasedFromPool | the number of threads released from the pool. |
std::unique_ptr<WorkerThread> alexaClientSDK::avsCommon::utils::threading::ThreadPool::obtainWorker | ( | ) |
Obtain a worker thread to operate on.
This method returns a reference to worker object. If thread pool has worker objects in the pool, it returns one of them. If thread pool is empty, a newly constructed object is returned.
void alexaClientSDK::avsCommon::utils::threading::ThreadPool::releaseWorker | ( | std::unique_ptr< WorkerThread > | workerThread | ) |
Release a worker
workerThread | the worker thread to release. |
void alexaClientSDK::avsCommon::utils::threading::ThreadPool::setMaxThreads | ( | size_t | maxThreads | ) |
Sets the maximum threads the pool should hold.
maxThreads | the max threads the pool should hold. If 0 is passed, the maximum threads will be set to 1, since the ThreadPool implementation does not support less than a single thread. |
AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0