AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Public Member Functions | Static Public Member Functions | List of all members
alexaClientSDK::avsCommon::utils::threading::ThreadPool Class Reference

#include <ThreadPool.h>

Public Member Functions

 ThreadPool (size_t maxThreads=DEFAULT_MAX_THREAD_POOL_THREADS)
 
 ~ThreadPool ()
 
std::unique_ptr< WorkerThreadobtainWorker ()
 
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< ThreadPoolgetDefaultThreadPool ()
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ ThreadPool()

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.

◆ ~ThreadPool()

alexaClientSDK::avsCommon::utils::threading::ThreadPool::~ThreadPool ( )

Destructs the thread pool.

Member Function Documentation

◆ getDefaultThreadPool()

static std::shared_ptr<ThreadPool> alexaClientSDK::avsCommon::utils::threading::ThreadPool::getDefaultThreadPool ( )
static

Obtain a shared pointer to the default singleton thread pool.

Returns
a shared pointer to the thread pool.

◆ getMaxThreads()

size_t alexaClientSDK::avsCommon::utils::threading::ThreadPool::getMaxThreads ( )

Obtain the current maximum threads for the thread pool.

Returns
the max threads the thread pool is currently set to hold.

◆ getStats()

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

Parameters
threadsCreatedthe total number of threads the pool has created.
threadsObtainedthe number of threads obtained from the pool.
threadsReleasedToPoolthe number of threads released from the caller into the pool.
threadsReleasedFromPoolthe number of threads released from the pool.

◆ obtainWorker()

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.

Returns
Worker thread object.

◆ releaseWorker()

void alexaClientSDK::avsCommon::utils::threading::ThreadPool::releaseWorker ( std::unique_ptr< WorkerThread workerThread)

Release a worker

Parameters
workerThreadthe worker thread to release.

◆ setMaxThreads()

void alexaClientSDK::avsCommon::utils::threading::ThreadPool::setMaxThreads ( size_t  maxThreads)

Sets the maximum threads the pool should hold.

Parameters
maxThreadsthe 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.

The documentation for this class was generated from the following file:

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