You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: design/mailbox_design.md
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,7 +193,20 @@ You only get ownership back when you `receive()` it or get it from `close()`.
193
193
Always wait for all threads to finish (`thread.join`) before you free the mailbox itself.
194
194
The mailbox must stay alive as long as any thread can still access it.
195
195
196
-
### 4. nbio loop initialization
196
+
### 4. Message lifetime
197
+
Never use stack-allocated messages for inter-thread communication.
198
+
The stack frame can be freed before the receiving thread reads the message.
199
+
200
+
Three ownership patterns:
201
+
202
+
1.**Heap**: `new` to allocate, `free` after receive. Simple. Good for low-frequency use.
203
+
2.**Pool**: `pool.get` / `pool.put`. High-throughput recycling. Zero allocations during the run.
204
+
3.**MASTER**: one struct owns both pool and mailbox. One shutdown call handles everything.
205
+
206
+
"Zero copies" means mbox does not copy message data. It does not mean zero allocations.
207
+
You still allocate message objects. mbox just links them.
208
+
209
+
### 5. nbio loop initialization
197
210
On some platforms (like macOS/kqueue), `nbio.wake_up` requires that the event loop has been ticked at least once to register the internal wake-up event in the kernel.
198
211
199
212
Before starting any threads that might call `send_to_loop` or `close_loop`, call `nbio.tick(0)` on the loop thread. This ensures the loop is fully initialized and ready to receive wake-up signals from other threads.
0 commit comments