@@ -110,23 +110,79 @@ target_link_libraries(your_target PRIVATE threadpool)
110110
111111## Architecture
112112
113+ ``` mermaid
114+ flowchart TB
115+ subgraph Client[Client Application]
116+ Submit[submit task]
117+ Future[get future]
118+ end
119+
120+ subgraph Pool[ThreadPool]
121+ subgraph Workers[Worker Threads]
122+ W1[Worker 1]
123+ W2[Worker 2]
124+ WN[Worker N]
125+ end
126+
127+ subgraph Queues[Task Queues]
128+ GQ[Global Priority Queue]
129+ LQ1[Local Queue 1]
130+ LQ2[Local Queue 2]
131+ LQN[Local Queue N]
132+ end
133+ end
134+
135+ Submit --> GQ
136+ GQ --> W1 & W2 & WN
137+ W1 --> LQ1
138+ W2 --> LQ2
139+ WN --> LQN
140+
141+ LQ1 -.->|steal| W2
142+ LQ2 -.->|steal| WN
143+ LQN -.->|steal| W1
144+
145+ W1 & W2 & WN --> Future
146+ ```
147+
148+ ### Work-Stealing Flow
149+
150+ ``` mermaid
151+ sequenceDiagram
152+ participant C as Client
153+ participant GQ as Global Queue
154+ participant W1 as Worker 1
155+ participant W2 as Worker 2
156+ participant LQ1 as Local Queue 1
157+
158+ C->>GQ: submit(task)
159+ GQ->>W1: pop task
160+ W1->>W1: execute task
161+
162+ Note over W2: Queue empty
163+ W2->>LQ1: steal task
164+ LQ1->>W2: return task
165+ W2->>W2: execute stolen task
166+
167+ W1->>C: future.get() returns
113168```
114- ┌─────────────────────────────────────────────────────────────┐
115- │ ThreadPool │
116- ├─────────────────────────────────────────────────────────────┤
117- │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
118- │ │ Worker 1 │ │ Worker 2 │ │ Worker N │ │
119- │ │ ┌─────────┐ │ │ ┌─────────┐ │ │ ┌─────────┐ │ │
120- │ │ │Local Q │◄┼──┼─┤ STEAL │◄┼──┼─┤Local Q │ │ │
121- │ │ └─────────┘ │ │ └─────────┘ │ │ └─────────┘ │ │
122- │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
123- │ └────────────────┼────────────────┘ │
124- │ ▼ │
125- │ ┌─────────────────────────────────────────────────────┐ │
126- │ │ Global Priority Queue │ │
127- │ │ [P:0] [P:1] [P:2] [P:5] [P:10] ← Higher priority │ │
128- │ └─────────────────────────────────────────────────────┘ │
129- └─────────────────────────────────────────────────────────────┘
169+
170+ ---
171+
172+ ### Task Lifecycle
173+
174+ ``` mermaid
175+ stateDiagram-v2
176+ [*] --> Submitted: submit()
177+ Submitted --> Queued: Added to queue
178+ Queued --> Running: Worker picks up
179+ Running --> Completed: Success
180+ Running --> Failed: Exception
181+ Completed --> [*]: future.get()
182+ Failed --> [*]: future.get() throws
183+
184+ Queued --> Cancelled: shutdown_now()
185+ Cancelled --> [*]
130186```
131187
132188---
0 commit comments