A bugfix that enabled the proposed v2 of the scoped thread API recently
landed, so its now possible to write libraries for scoped threads.
This is my take on it, a scoped threadpool that runs on stable rust:
(Reddit discussion: https://www.reddit.com/r/rust/comments/3hyv3i/safe_stable_and_scoped_threadpool/)
PSA: Only has a safe interface on nightly right now, see reddit thread.
- Thread creation is independent from job execution, so
you can cache the pool itself, even in a global variable. (It combines nicely with lazy_static for example)
- Safe interface due to not using RAII for joining threads. (If there
is other unsafety with scoped threads in general I’m not currently aware
- Stable interface due to using thread::spawn internally. (The only unsafe block is for transmuting a job to 'static lifetime)
- Jobs can access data of any lifetime outside of the scope during which they run, re-enabling parallel processing of stack data.
This is just the bare bone functionality right now, and no care has
been given yet to optimize it in particular (and I’m missing the
expertise for it), so feel free to offer suggestions for improvements.