载入中...
搜索中...
未找到
ThreadPool.h
浏览该文件的文档.
1#pragma once
2
3#include "thread/Task.h"
4
5#include <condition_variable>
6#include <cstddef>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <queue>
11#include <string>
12#include <thread>
13#include <vector>
14
15namespace eve {
16namespace thread {
17
18class Channel;
19
25public:
26 explicit ThreadPool(int workerCount);
28
29 ThreadPool(const ThreadPool &) = delete;
30 ThreadPool &operator=(const ThreadPool &) = delete;
31
32 int getWorkerCount() const;
33 int getPendingCount() const;
34 bool isRunning() const;
35
37 Task *submit(std::function<void()> fn);
38
40 Task *submitSleep(int ms);
41
43 Task *submitPush(Channel *channel, std::string message, int delayMs = 0);
44
49 Task *submitPost(std::string name, std::string data = "", int delayMs = 0);
50
52 void waitAll();
53
55 void stop();
56
57private:
58 struct State;
59
60 static void workerMain(std::shared_ptr<State> state);
61 void stopImpl(bool allowWorkerCaller);
62
63 int workerCount_ = 0;
64 std::vector<std::thread> workers_;
65 std::shared_ptr<State> state_;
66 std::mutex lifecycleMu_;
67};
68
69} // namespace thread
70} // namespace eve
JobSystemThreadPool::State * state
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
Thread-safe message queue (love2d-style Channel). Values are strings so the API stays overload-free f...
Definition Channel.h:17
A job executed by a ThreadPool worker. Status strings (no enums): "pending" | "running" | "done" | "f...
Definition Task.h:16
Fixed-size worker pool. Owns worker std::threads; tasks run FIFO. Squirrel VM is not thread-safe — do...
Definition ThreadPool.h:24
void waitAll()
Block until idle. Throws when called by a worker belonging to this pool.
int getPendingCount() const
ThreadPool(const ThreadPool &)=delete
ThreadPool & operator=(const ThreadPool &)=delete
Task * submitSleep(int ms)
Sleep on a worker, then mark done — useful from scripts / tests.
void stop()
Stop accepting work and join workers. Worker calls are rejected. Idempotent.
Task * submitPost(std::string name, std::string data="", int delayMs=0)
Sleep, then post an Event on the main queue (thread-safe). Scripts poll via event....
Task * submitPush(Channel *channel, std::string message, int delayMs=0)
Sleep, then push a message onto a channel (cross-thread signalling).
Task * submit(std::function< void()> fn)
Submit a C++ callable. Caller owns the Task wrapper; work owns its shared state.
Definition Build.cpp:11