SpiecsEngine
 
Loading...
Searching...
No Matches

◆ ThreadFunc() [1/2]

void Spices::VulkanCmdThreadPool::ThreadFunc ( Thread< VkCommandBuffer > * thread)

Thread Function.

Parameters
[in]threadThread Entity.

Name thread.

Exit.

Try recovery unused threads.

Rink First, Thread Tasks.

Rink Second, Pool Tasks.

execute task.

Name thread.

Exit.

Try recovery unused threads.

Rink First, Thread Tasks.

Rink Second, Pool Tasks.

execute task.

Definition at line 69 of file VulkanCmdThreadPool.cpp.

70 {
72
76 std::stringstream ss;
77 ss << m_PoolName << thread->GetId();
78
80
81 auto lastTime = std::chrono::high_resolution_clock::now();
82
83 for (;;)
84 {
85 Task task;
86 {
87 std::unique_lock<std::mutex> lock(m_Mutex);
88
89 while ((m_Tasks.load() == 0 && thread->GetThreadTasksCount() == 0) || m_IsSuspend)
90 {
94 if (!m_IsPoolRunning)
95 {
96 m_Threads.erase(thread->GetId());
98 m_ExitCond.notify_all();
99 return;
100 }
101
103 {
104 if (m_NotEmpty.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout)
105 {
106 auto now = std::chrono::high_resolution_clock::now();
107 auto dur = std::chrono::duration_cast<std::chrono::seconds>(now - lastTime);
108
112 if (dur.count() >= m_ThreadIdleTimeOut && m_Threads.size() > m_InitThreadSize)
113 {
114 m_Threads.erase(thread->GetId());
116 --m_NThreads;
117 return;
118 }
119 }
120 }
121 else
122 {
123 m_NotEmpty.wait(lock);
124 }
125 }
126
130 task = thread->RequireTask();
131
135 if (!task)
136 {
137 task = m_TaskQueue.front();
138 m_TaskQueue.pop();
139 thread->SetThreadInTask(true);
140 --m_Tasks;
141 }
142
144 }
145
146 if (m_Tasks > 0)
147 {
148 m_NotEmpty.notify_all();
149 }
150
154 if (task != nullptr)
155 {
156 VkCommandBuffer cmdBuffer = CreateParallelCommandBuffers(thread->GetId());
157 task(cmdBuffer);
158 thread->SetThreadInTask(false);
160 m_IdleCond.notify_all();
161 }
162 else
163 {
164 thread->SetThreadInTask(false);
166 m_IdleCond.notify_all();
167 }
168
169 lastTime = std::chrono::high_resolution_clock::now();
170 }
171 }
#define SPICES_PROFILE_ZONE
static bool SetThreadName(const std::string &name)
Set Thread name.
bool m_IsSuspend
True if needs suspend on executing the task.
std::atomic_int m_IdleThreadSize
Idled thread size.
std::queue< Task > m_TaskQueue
Task Queue.
std::atomic_int m_Tasks
Number of tasks;.
std::condition_variable m_ExitCond
Thread pool Exit Condition.
PoolMode m_PoolMode
Thread Pool Run Mode.
std::function< void(Params...)> Task
Thread Function lambda Object.
std::mutex m_Mutex
Mutex for thread safe.
std::unordered_map< uint32_t, std::unique_ptr< Thread< Params... > > > m_Threads
Threads Container.
std::string m_PoolName
This ThreadPool Name.
std::condition_variable m_NotEmpty
Task Queue not empty.
uint32_t m_InitThreadSize
Initialized thread size.
uint32_t m_ThreadIdleTimeOut
thread idle time out.
std::atomic_int m_NThreads
Threads Count.
std::condition_variable m_IdleCond
Thread pool thread idle Condition.
std::atomic_bool m_IsPoolRunning
True if this thread pool is in use.
VkCommandBuffer CreateParallelCommandBuffers(uint32_t threadId)
Create second CommandBuffer in rhi threadPool thread.

References Spices::ThreadLibrary::SetThreadName().