32 lines
No EOL
1 KiB
C++
Executable file
32 lines
No EOL
1 KiB
C++
Executable file
//SPDX-License-Identifier: LGPL-3.0-only
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Thread:ThreadPool;
|
|
import std;
|
|
|
|
namespace Crafter {
|
|
struct ThreadStorage {
|
|
std::vector<std::function<void(void)>> tasks;
|
|
std::vector<std::function<void(void)>> buffer;
|
|
std::condition_variable cv;
|
|
std::mutex mutex;
|
|
std::atomic<unsigned int> taskCount;
|
|
std::thread thread;
|
|
};
|
|
|
|
export class ThreadPool {
|
|
public:
|
|
static inline std::atomic<bool> stopped;
|
|
static void Start(int maxThreads = -1);
|
|
static void Stop();
|
|
static void Enqueue(std::function<void(void)> task);
|
|
static void Enqueue(std::vector<std::function<void(void)>> tasks);
|
|
static void JoinPool();
|
|
static inline unsigned int threadCount;
|
|
static inline std::atomic<unsigned int> activeThreads;
|
|
private:
|
|
static inline std::unordered_map<std::thread::id, ThreadStorage*> threadIdMap;
|
|
static inline ThreadStorage* threadStorage;
|
|
static void WaitAndExecuteTask(ThreadStorage* threadStorage);
|
|
};
|
|
} |