hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
codegenTaskDispatcher.hpp
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2026/2/8.
3//
4
5#ifndef HOSHI_LANG_CODEGEN_TASK_DISPATCHER_HPP
6#define HOSHI_LANG_CODEGEN_TASK_DISPATCHER_HPP
7
8#include <atomic>
9#include <condition_variable>
10#include <functional>
11#include <mutex>
12#include <queue>
13#include <thread>
14#include <vector>
15
16namespace yoi {
17
19 public:
20 explicit CodegenTaskDispatcher(size_t threadCount = 0);
22
23 // Disable copy and move
26
31 void dispatch(std::function<void()> task);
32
36 void wait();
37
38 private:
39 void workerLoop();
40
41 std::vector<std::thread> workers;
42 std::queue<std::function<void()>> taskQueue;
43
44 std::mutex queueMutex;
45 std::condition_variable queueCondition;
46
47 std::mutex waitMutex;
48 std::condition_variable waitCondition;
49
50 std::atomic<size_t> activeTasks{0};
51 std::atomic<bool> stop{false};
52 };
53
54} // namespace yoi
55
56#endif // HOSHI_LANG_CODEGEN_TASK_DISPATCHER_HPP
std::condition_variable queueCondition
void dispatch(std::function< void()> task)
Dispatch a task to the thread pool.
CodegenTaskDispatcher(const CodegenTaskDispatcher &)=delete
std::queue< std::function< void()> > taskQueue
void wait()
Wait for all dispatched tasks to complete.
CodegenTaskDispatcher & operator=(const CodegenTaskDispatcher &)=delete
std::vector< std::thread > workers
std::condition_variable waitCondition