https://github.com/timstr/threadpool
A rust threadpool with parallel work-stealing "foreach" with non-static lifetime support
https://github.com/timstr/threadpool
multithreading parallel rust threading work-stealing
Last synced: 12 months ago
JSON representation
A rust threadpool with parallel work-stealing "foreach" with non-static lifetime support
- Host: GitHub
- URL: https://github.com/timstr/threadpool
- Owner: timstr
- License: mit
- Created: 2023-08-26T16:35:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-21T06:30:23.000Z (about 2 years ago)
- Last Synced: 2025-01-22T02:31:21.265Z (about 1 year ago)
- Topics: multithreading, parallel, rust, threading, work-stealing
- Language: Rust
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# threadpool
A basic threadpool that uses work-stealing and allows closures with non-static references.
Currently only `foreach` is implemented. This function takes a mutable slice and closure which mutates an element of the slice, and dispatches this to each thread in the pool on a first-come-first-serve basis. `foreach` returns once all elements in the slice have been visited.
Example usage:
```rust
let mut data: Vec = (0..65536).collect();
let mut threadpool = ThreadPool::new(1);
let offset: usize = 10;
threadpool.foreach(&mut data, |x| {
*x = *x + offset; // <---------- 'offset' is borrowed from multiple threads here!
});
assert!(data.iter().enumerate().all(|(i, x)| *x == i + 10));
```
## Is this library safe and sound?
Um, probably? Thread-safe atomics, channels, and barriers are used to handle all synchronization and the included tests run and pass. However, I don't have a rigorous proof that it is guaranteed to be memory safe, and part of the implementation relies on `std::mem::transmute` to cast away the lifetime bound on the closure being passed to other threads. This is a known-workaround for writing threading primitives as mentioned in item 3 of https://github.com/rust-lang/rust/pull/55043. The use of a shared one-off barrier inside of `foreach` serves to ensure that all threads have finished using and dropped the closure before `foreach` returns.