#thread #timeout #join #group #attempt #manage #unit

threadgroup

Manage groups of threads as one unit, join the first one that finishes, timeout on join attempts

1 unstable release

Uses old Rust 2015

0.1.0 Feb 2, 2018

#900 in Concurrency

Download history 15/week @ 2024-07-20 49/week @ 2024-07-27 25/week @ 2024-08-03 157/week @ 2024-08-10 67/week @ 2024-08-17 96/week @ 2024-08-24 76/week @ 2024-08-31 55/week @ 2024-09-07 92/week @ 2024-09-14 67/week @ 2024-09-21 49/week @ 2024-09-28 123/week @ 2024-10-05 99/week @ 2024-10-12 63/week @ 2024-10-19 65/week @ 2024-10-26 63/week @ 2024-11-02

297 downloads per month

MIT license

14KB
129 lines

ThreadGroup

This crate handles a group of threads (whose closures have the same return type) as one unit, letting you join() or join_timeout() on the first thread of the group that is ready. Internally, it uses a channel to notify about finished threads.

This is useful when you want to check whether any one of your threads has panicked or returned early, for error-handling or for progress report. This isn't possible with std::thread::JoinHanlde.join() because it is a blocking call, so thread 2 might panic or finish while you're waiting indefinitely on thread 1.

See also

If ThreadGroup isn't what you needed after all and can't be improved to suit your needs, have a look at these other crates :

  • Thread_tryjoin gives the same benefit as ThreadGroup.join_timeout() with an API that is closer to std's. But it depends on a Linux-only API, and only handles one thread at a time (so that try_join()ing a lot of thread will end up wasting either time or CPU).
  • Thread-control and Runloop let you stop threads, and test without blocking if they panicked or finished. Again, this only handles one thread at a time.
  • Rayon is a higher-level abstraction to spread a computation over multiple threads.
  • Thread pools are a common idiom to handle short computations without repeatedly paying the high setup cost.
  • Future isn't related to threads but is often a better way to handle "start task B whenever task A finishes" algorythms.

Contributing

Please create issues and send pull request via Github.

Threadgroup is licensed as MIT. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as above, without any additional terms or conditions.

No runtime deps