Commit 621c647
std.concurrency: Add join() to prevent thread resource leaks (#10894)
Add a new `join(Tid)` function that allows explicit joining of threads
created by spawn(). This prevents OS resource leaks in long-running
applications that create many short-lived threads.
Without calling join(), thread stacks (~8 MB each on typical systems)
accumulate in virtual memory for the lifetime of the process because:
- Threads created by spawn() are never explicitly joined
- Thread objects remain in the global thread list until process exit
- pthread cannot free thread stacks until pthread_join() is called
The new join() function:
- Blocks until the thread completes
- Releases OS resources (stack, TLS) via pthread_join()
- Throws ThreadError if used on scheduler-created threads
- Throws ThreadError if the thread was already joined
Testing shows:
- Without join: ~8,200 KB virtual memory per thread (stacks leak)
- With join: ~38 KB per thread (stacks properly freed)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude <noreply@anthropic.com>1 parent 5977f4a commit 621c647
1 file changed
+63
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
622 | 623 | | |
623 | 624 | | |
624 | 625 | | |
| 626 | + | |
625 | 627 | | |
626 | 628 | | |
627 | 629 | | |
| |||
1197 | 1199 | | |
1198 | 1200 | | |
1199 | 1201 | | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
1200 | 1262 | | |
1201 | 1263 | | |
1202 | 1264 | | |
| |||
2426 | 2488 | | |
2427 | 2489 | | |
2428 | 2490 | | |
| 2491 | + | |
2429 | 2492 | | |
2430 | 2493 | | |
2431 | 2494 | | |
| |||
0 commit comments