SpiecsEngine
 
Loading...
Searching...
No Matches

◆ SubmitPoolTask()

template<typename ... Params>
template<typename Func , typename ... Args>
auto Spices::ThreadPool_Basic< Params >::SubmitPoolTask ( Func && func,
Args &&... args ) -> std::future<decltype(func(std::forward<Args>(args)...))>
inlineinherited

Submit a task to task queue, and wait for a idle thread to execute it.

Template Parameters
FuncTask Function.
ArgsTask Function Parameter.
Returns
Returns task function return value as a future.

pack function pointer with packaged_task and get future.

pack task as a lambda and submit it to queue.

Expand threads container if in MODE_CACHED.

Definition at line 703 of file ThreadPoolBasic.h.

704 {
706
707 using RType = decltype(func(args...));
708
712 auto task = std::make_shared<std::packaged_task<RType()>>(std::bind(std::forward<Func>(func), std::forward<Args>(args)...));
713 std::future<RType> result = task->get_future();
714
715 {
716 std::unique_lock<std::mutex> lock(m_Mutex);
717
721 m_TaskQueue.emplace([task]() {(*task)(); });
722 ++m_Tasks;
723
728 {
729 for (uint32_t i = 0; i < THREAD_MAX_THRESHHOLD; i++)
730 {
731 if (m_Threads.find(i) == m_Threads.end())
732 {
733 auto ptr = std::make_unique<Thread<>>(std::bind(&ThreadPool_Basic::ThreadFunc, this, std::placeholders::_1), i);
734 uint32_t threadId = ptr->GetId();
735
736 ptr->Start();
737 m_Threads.emplace(threadId, std::move(ptr));
738
740 ++m_NThreads;
741
742 break;
743 }
744 }
745 }
746 }
747
748 m_NotEmpty.notify_all();
749
750 return result;
751 }
#define SPICES_PROFILE_ZONE
std::atomic_int m_IdleThreadSize
Idled thread size.
std::queue< Task > m_TaskQueue
Task Queue.
std::atomic_int m_Tasks
Number of tasks;.
PoolMode m_PoolMode
Thread Pool Run Mode.
std::mutex m_Mutex
Mutex for thread safe.
std::unordered_map< uint32_t, std::unique_ptr< Thread< Params... > > > m_Threads
Threads Container.
std::condition_variable m_NotEmpty
Task Queue not empty.
void ThreadFunc(Thread<> *thread)
Thread Function.
std::atomic_int m_NThreads
Threads Count.
const uint32_t THREAD_MAX_THRESHHOLD

References Spices::ThreadPool_Basic< Params >::m_NotEmpty, and Spices::THREAD_MAX_THRESHHOLD.