6#include "LkEngine/Core/Thread.h"
9#if defined(LK_PLATFORM_WINDOWS)
10# include "LkEngine/Platform/Windows/WindowsThread.h"
11namespace LkEngine {
using TThread = LThread<LWindowsThread>; }
12#elif defined(LK_ENGINE_GCC) || defined(LK_ENGINE_CLANG)
18 struct FThreadStartArgs
20 bool bRunAfterCreation =
false;
26 ~LThreadManager() =
default;
28 template<
typename TCallable,
typename... TArgs>
29 void CreateThread(
const FThreadStartArgs& ThreadStartArgs, TCallable&& Func, TArgs&&... Args);
33 int GetThreadPoolSize()
const
35 return static_cast<int>(ThreadPool.size());
38 void StartThread(
const uint8_t ThreadIndex)
40 ThreadPool.at(ThreadIndex)->Run();
43 static void SubmitFunctionToThread(FThreadData& ThreadData,
const std::function<
void()> Function)
45 LK_CORE_DEBUG_TAG(
"ThreadManager",
"Adding function to queue, new queue size: {}", ThreadData.CommandQueue.size() + 1);
46 ThreadData.CommandQueue.push(Function);
49 static LThreadManager& Get();
52 std::vector<std::shared_ptr<TThread>> ThreadPool{};
55 template<
typename TCallable,
typename... TArgs>
56 inline void LThreadManager::CreateThread(
const FThreadStartArgs& ThreadStartArgs, TCallable&& Function, TArgs&&... Args)
58 LK_CORE_DEBUG_TAG(
"ThreadManager",
"Creating new thread, indexed={}", ThreadPool.size());
60 std::shared_ptr<TThread> Thread = std::make_shared<TThread>(std::forward<TCallable>(Function), std::forward<TArgs>(Args)...);
61 ThreadPool.push_back(Thread);
63 if (ThreadStartArgs.bRunAfterCreation)
69 static void Thread_SubmitCommand(FThreadData& ThreadData,
const std::function<
void()> Function)
71 ThreadData.CommandQueue.push(Function);
72 LK_CORE_DEBUG_TAG(
"ThreadManager",
"Added command to command queue, new queue size: {}", ThreadData.CommandQueue.size());
75 static void ThreadFunction_TestCommandQueue(FThreadData& ThreadData)
77 std::queue<std::function<void()>>& CommandQueue = ThreadData.CommandQueue;
79 while (ThreadData.bIsRunning)
82 while (CommandQueue.size() > 0)
85 std::function<void()> Function = CommandQueue.front();