38 template<
typename... ArgTypes>
using TimeElapsedHandler = std::function<void(ArgTypes...)>;
40 using TimeElapsedHandlerNoArgs = std::function<void(
void)>;
42 const double percentage{ 10.0f / 100.0f };
44 class DiscordCoreAPI_Dll ThreadPool {
46 ThreadPool& operator=(
const ThreadPool&) =
delete;
48 ThreadPool(
const ThreadPool&) =
delete;
50 ThreadPool() noexcept = default;
52 static std::
string storeThread(TimeElapsedHandlerNoArgs timeElapsedHandler, int64_t timeInterval);
54 template<typename... ArgTypes> static
void executeFunctionAfterTimePeriod(TimeElapsedHandler<ArgTypes...> timeElapsedHandler,
55 int64_t timeDelay,
bool blockForCompletion, ArgTypes... args) {
56 std::jthread thread = std::jthread([=](std::stop_token token) {
57 StopWatch stopWatch{ Milliseconds{ timeDelay } };
58 stopWatch.resetTimer();
59 if (
static_cast<int64_t
>(std::ceil(
static_cast<double>(timeDelay) * percentage)) <= timeDelay &&
60 static_cast<int64_t
>(std::ceil(
static_cast<double>(timeDelay) * percentage)) > 0) {
61 std::this_thread::sleep_for(Milliseconds{
static_cast<int64_t
>(std::ceil(
static_cast<double>(timeDelay) * percentage)) });
63 while (!stopWatch.hasTimePassed() && !token.stop_requested()) {
64 std::this_thread::sleep_for(1ms);
66 if (token.stop_requested()) {
69 timeElapsedHandler(args...);
70 if (token.stop_requested()) {
74 if (blockForCompletion) {
75 if (thread.joinable()) {
78 }
else if (thread.joinable()) {
83 void stopThread(
const std::string& key);
85 ~ThreadPool() noexcept = default;
88 static std::unordered_map<std::
string, std::jthread> threads;
92namespace DiscordCoreInternal {
94 struct DiscordCoreAPI_Dll WorkerThread {
95 WorkerThread& operator=(WorkerThread&& other)
noexcept;
97 WorkerThread() noexcept = default;
99 ~WorkerThread() noexcept = default;
101 std::atomic_bool areWeCurrentlyWorking{};
102 std::jthread thread{};
105 class DiscordCoreAPI_Dll CoRoutineThreadPool {
109 CoRoutineThreadPool();
111 void submitTask(std::coroutine_handle<> coro)
noexcept;
114 std::unordered_map<int64_t, WorkerThread> workerThreads{};
115 std::deque<std::coroutine_handle<>> coroutineHandles{};
116 const std::atomic_int64_t threadCount{};
117 std::atomic_int64_t coroHandleCount{};
118 std::shared_mutex workerAccessMutex{};
119 std::atomic_int64_t currentCount{};
120 std::atomic_int64_t currentIndex{};
121 std::mutex coroHandleAccessMutex{};
123 void threadFunction(std::stop_token token, int64_t index);
The main namespace for this library.
DiscordCoreClient - The main class for this library.