14#ifndef AUTONOMYTHREAD_H
15#define AUTONOMYTHREAD_H
23#include <condition_variable>
312 void RunPool(
const unsigned int nNumTasksToQueue,
const unsigned int nNumThreads = 2,
const bool bForceStopCurrentThreads =
false)
315 if (
m_thPool.get_thread_count() != nNumThreads)
329 else if (bForceStopCurrentThreads)
341 for (
unsigned int i = 0; i < nNumTasksToQueue; ++i)
348 this->PooledLinearCode();
384 void RunDetachedPool(
const unsigned int nNumTasksToQueue,
const unsigned int nNumThreads = 2,
const bool bForceStopCurrentThreads =
false)
387 if (
m_thPool.get_thread_count() != nNumThreads)
401 else if (bForceStopCurrentThreads)
413 for (
unsigned int i = 0; i < nNumTasksToQueue; ++i)
451 template<
typename N,
typename F>
452 void ParallelizeLoop(
const int nNumThreads,
const N tTotalIterations, F&& tLoopFunction)
459 [&tLoopFunction](
const int nStart,
const int nEnd)
462 tLoopFunction(nStart, nEnd);
502 return (
m_thPool.get_tasks_total() <= 0);
556 std::vector<T> vResults;
562 vResults.emplace_back(fResult.get());
627 std::chrono::_V2::system_clock::time_point tmStartTime;
636 tmStartTime = std::chrono::high_resolution_clock::now();
643 if (m_nMainThreadMaxIterationPerSecond > 0)
646 std::chrono::_V2::system_clock::time_point tmEndTime = std::chrono::high_resolution_clock::now();
648 std::chrono::microseconds tmElapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(tmEndTime - tmStartTime);
655 std::this_thread::sleep_for(std::chrono::microseconds(nSleepTime));
BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library....
Define and implement the IPS class.
void Join()
Waits for thread to finish executing and then closes thread. This method will block the calling code ...
Definition AutonomyThread.hpp:198
void ParallelizeLoop(const int nNumThreads, const N tTotalIterations, F &&tLoopFunction)
Given a ref-qualified looping function and an arbitrary number of iterations, this method will divide...
Definition AutonomyThread.hpp:452
void RunPool(const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false)
When this method is called, it starts/adds tasks to a thread pool that runs nNumTasksToQueue copies o...
Definition AutonomyThread.hpp:312
BS::thread_pool< BS::tp::pause > m_thPool
Definition AutonomyThread.hpp:595
AutonomyThreadState
Definition AutonomyThread.hpp:46
@ eStopped
Definition AutonomyThread.hpp:50
@ eStopping
Definition AutonomyThread.hpp:49
@ eRunning
Definition AutonomyThread.hpp:48
@ eStarting
Definition AutonomyThread.hpp:47
virtual T PooledLinearCode()=0
int GetMainThreadMaxIPS() const
Accessor for the Main Thread Max I P S private member.
Definition AutonomyThread.hpp:579
BS::pr m_ePoolThreadPriority
Definition AutonomyThread.hpp:591
void SetMainThreadIPSLimit(int nMaxIterationsPerSecond=0)
Mutator for the Main Thread Max I P S private member.
Definition AutonomyThread.hpp:515
AutonomyThread()
Construct a new Autonomy Thread object.
Definition AutonomyThread.hpp:75
std::vector< std::future< T > > m_vPoolReturns
Definition AutonomyThread.hpp:597
int GetPoolNumOfThreads()
Accessor for the Pool Num Of Threads private member.
Definition AutonomyThread.hpp:529
std::mutex m_muThreadRunningConditionMutex
Definition AutonomyThread.hpp:600
std::vector< T > GetPoolResults()
Accessor for the Pool Results private member. The action of getting results will destroy and remove t...
Definition AutonomyThread.hpp:553
std::atomic_bool m_bStopThreads
Definition AutonomyThread.hpp:598
BS::thread_pool< BS::tp::pause > m_thMainThread
Definition AutonomyThread.hpp:594
void RequestStop()
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the...
Definition AutonomyThread.hpp:182
bool Joinable() const
Check if the code within the thread and all pools created by it are finished executing and the thread...
Definition AutonomyThread.hpp:219
BS::pr m_eMainThreadPriority
Definition AutonomyThread.hpp:590
virtual void ThreadedContinuousCode()=0
std::condition_variable m_cdThreadRunningCondition
Definition AutonomyThread.hpp:601
std::atomic< AutonomyThreadState > m_eThreadState
Definition AutonomyThread.hpp:599
IPS & GetIPS()
Accessor for the Frame I P S private member.
Definition AutonomyThread.hpp:242
int m_nMainThreadMaxIterationPerSecond
Definition AutonomyThread.hpp:602
IPS m_IPS
Definition AutonomyThread.hpp:278
int GetPoolQueueLength()
Accessor for the Pool Queue Size private member.
Definition AutonomyThread.hpp:539
void ClearPoolQueue()
Clears any tasks waiting to be ran in the queue, tasks currently running will remain running.
Definition AutonomyThread.hpp:477
void Start()
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCo...
Definition AutonomyThread.hpp:129
void SetMainThreadPriority(AutonomyThreadPriority ePriority)
Set the OS priority for the main continuous thread.
Definition AutonomyThread.hpp:254
AutonomyThreadPriority
Definition AutonomyThread.hpp:56
@ eHigh
Definition AutonomyThread.hpp:60
@ eHighest
Definition AutonomyThread.hpp:62
@ eNormal
Definition AutonomyThread.hpp:59
@ eLow
Definition AutonomyThread.hpp:58
@ eLowest
Definition AutonomyThread.hpp:57
void SetPoolThreadPriority(AutonomyThreadPriority ePriority)
Set the OS priority for the highly parallelized pool threads.
Definition AutonomyThread.hpp:266
void RunThread(std::atomic_bool &bStopThread)
This method is ran in a separate thread. It is a middleware between the class member thread and the u...
Definition AutonomyThread.hpp:624
virtual ~AutonomyThread()
Destroy the Autonomy Thread object. If the parent object or main thread is destroyed or exited while ...
Definition AutonomyThread.hpp:93
void RunDetachedPool(const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false)
When this method is called, it starts a thread pool full of threads that don't return std::futures (l...
Definition AutonomyThread.hpp:384
bool PoolJoinable() const
Check if the internal pool threads are done executing code and the queue is empty.
Definition AutonomyThread.hpp:499
void JoinPool()
Waits for pool to finish executing tasks. This method will block the calling code until thread is fin...
Definition AutonomyThread.hpp:487
AutonomyThreadState GetThreadState() const
Accessor for the Threads State private member.
Definition AutonomyThread.hpp:232
A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class.
Definition BS_thread_pool.hpp:1429
void detach_blocks(const T1 first_index, const T2 index_after_last, F &&block, const std::size_t num_blocks=0, const priority_t priority=0)
Parallelize a loop by automatically splitting it into blocks and submitting each block separately to ...
Definition BS_thread_pool.hpp:1531
void wait()
Wait for tasks to be completed. Normally, this function waits for all tasks, both those that are curr...
Definition BS_thread_pool.hpp:1964
This util class provides an easy way to keep track of iterations per second for any body of code.
Definition IPS.hpp:33
pr
An enum containing some pre-defined priorities for convenience.
Definition BS_thread_pool.hpp:398
@ lowest
Definition BS_thread_pool.hpp:399
@ high
Definition BS_thread_pool.hpp:402
@ highest
Definition BS_thread_pool.hpp:403
@ low
Definition BS_thread_pool.hpp:400
@ normal
Definition BS_thread_pool.hpp:401