|
| 1 | +#pragma once |
| 2 | +#include <functional> |
| 3 | +#include <vector> |
| 4 | +#include <memory> |
| 5 | +#include <atomic> |
| 6 | +#include <thread> |
| 7 | +#include <condition_variable> |
| 8 | +#include <queue> |
| 9 | +#include <future> |
| 10 | +#include <concepts> |
| 11 | + |
| 12 | +namespace tf { |
| 13 | + |
| 14 | +template<typename T> |
| 15 | +concept Task = requires(T t) { |
| 16 | + { t() } -> std::same_as<void>; |
| 17 | +}; |
| 18 | + |
| 19 | +class Scheduler { |
| 20 | +public: |
| 21 | + explicit Scheduler(size_t num_workers = std::thread::hardware_concurrency()); |
| 22 | + ~Scheduler(); |
| 23 | + |
| 24 | + // Non-copyable, non-movable |
| 25 | + Scheduler(const Scheduler&) = delete; |
| 26 | + Scheduler& operator=(const Scheduler&) = delete; |
| 27 | + |
| 28 | + // Submit a task and get a future |
| 29 | + template<Task F> |
| 30 | + auto submit(F&& func) -> std::future<decltype(func())>; |
| 31 | + |
| 32 | + // Submit with priority (higher = more urgent) |
| 33 | + template<Task F> |
| 34 | + auto submit(F&& func, int priority) -> std::future<decltype(func())>; |
| 35 | + |
| 36 | + // Parallel for |
| 37 | + void parallel_for(size_t begin, size_t end, std::function<void(size_t)> func); |
| 38 | + |
| 39 | + // Wait for all submitted tasks |
| 40 | + void wait_all(); |
| 41 | + |
| 42 | + // Statistics |
| 43 | + size_t worker_count() const { return workers_.size(); } |
| 44 | + size_t pending_tasks() const { return task_count_.load(); } |
| 45 | + size_t completed_tasks() const { return completed_count_.load(); } |
| 46 | + |
| 47 | +private: |
| 48 | + struct TaskItem { |
| 49 | + std::function<void()> func; |
| 50 | + int priority; |
| 51 | + bool operator<(const TaskItem& other) const { return priority < other.priority; } |
| 52 | + }; |
| 53 | + |
| 54 | + void worker_loop(size_t id); |
| 55 | + |
| 56 | + std::vector<std::thread> workers_; |
| 57 | + std::priority_queue<TaskItem> tasks_; |
| 58 | + std::mutex mutex_; |
| 59 | + std::condition_variable cv_; |
| 60 | + std::condition_variable cv_done_; |
| 61 | + std::atomic<bool> stop_{false}; |
| 62 | + std::atomic<size_t> task_count_{0}; |
| 63 | + std::atomic<size_t> completed_count_{0}; |
| 64 | +}; |
| 65 | + |
| 66 | +template<Task F> |
| 67 | +auto Scheduler::submit(F&& func) -> std::future<decltype(func())> { |
| 68 | + return submit(std::forward<F>(func), 0); |
| 69 | +} |
| 70 | + |
| 71 | +template<Task F> |
| 72 | +auto Scheduler::submit(F&& func, int priority) -> std::future<decltype(func())> { |
| 73 | + using ReturnType = decltype(func()); |
| 74 | + auto task = std::make_shared<std::packaged_task<ReturnType()>>(std::forward<F>(func)); |
| 75 | + auto future = task->get_future(); |
| 76 | + |
| 77 | + { |
| 78 | + std::lock_guard lock(mutex_); |
| 79 | + tasks_.push({[task]() { (*task)(); }, priority}); |
| 80 | + task_count_++; |
| 81 | + } |
| 82 | + cv_.notify_one(); |
| 83 | + |
| 84 | + return future; |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace tf |
0 commit comments