Safe, Stable and Scoped Threadpool

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:

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
    of it)
  • 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.

Cool! Is there any chance this could be merged with (or the other way around) the threadpool crate? It would be really nice to be able to re-use the same threadpool for both scoped and non-scoped (static lifetime) threads.

Sure, what feature are you missing specifically? :slight_smile:

pool.execute(...) instead of pool.scoped(|scope| scope.execute(...)).

I think the goal would be to have one, awesome, threadpool crate. Lots of similar crates should be avoided if possible.

A healthy ecosystem needs to have a bit of competition. While only having one crate that does a thing is conveninet in the short term, I think it hurts growth and leads to a worse situation in the long term.

I didn't say competition is bad. But collaboration is probably even better!