https://github.com/chopinsky/thread_pool
A Cargo package that can make you run your applications in thread-safe pools without efforts.
https://github.com/chopinsky/thread_pool
Last synced: 11 months ago
JSON representation
A Cargo package that can make you run your applications in thread-safe pools without efforts.
- Host: GitHub
- URL: https://github.com/chopinsky/thread_pool
- Owner: Chopinsky
- Created: 2018-03-21T03:50:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-11T23:50:31.000Z (almost 6 years ago)
- Last Synced: 2024-11-19T19:56:51.231Z (over 1 year ago)
- Language: Rust
- Size: 357 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Threads Pool
This package provides an easy way to create and manage thread pools, so you don't have to.
## How to use
In your project's `Cargo.toml`, add dependency:
```cargo
[dependencies]
threads_pool = "^0.2.0"
```
Then in your code:
```rust
extern crate threads_pool;
use std::time::Duration;
use std::thread::sleep;
use threads_pool::*;
fn main() {
// The pool lives as long as the `pool` variable, when pool goes out of
// the scope, the thread pool will be destroyed gracefully -- all threads
// will finish their current job and then garnered.
let pool = ThreadPool::new(8);
for num in 0..100 {
pool.execute(move || {
// Your code here...
println!("I'm in with: {}", num);
sleep(Duration::from_millis(10));
});
}
}
```
The package also provide a static pool which you can create once, and use it everywhere in your application.
This is called the `shared_mode`:
```rust
extern crate threads_pool;
use std::time::Duration;
use std::thread::sleep;
use threads_pool::shared_mode;
fn main() {
// Create the pool here, then you can use the pool everywhere. If run a task without
// creating the pool, it will be equivalent of calling 'thread::spawn' on the task.
shared_mode::initialize(8);
for num in 0..100 {
shared_mode::run(move || {
// Your code here...
println!("I'm in with: {}", num);
sleep(Duration::from_millis(10));
});
}
// The static pool must be closed, or unfinished threads will be destroyed prematurely and could cause panic in the
// running threads. this is different from the managed pool where it can know when to shutdown as the allocated pool
// object goes out of the scope.
shared_mode::close();
}
```