1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! ***tinypool*** is a simple thread pool implementation in Rust.
//!
//! A thread pool is a collection of threads that can be used to execute jobs concurrently.
//! The aim of this crate is to provide a simple, easy-to-use thread pool that can be used in any project.
//!
//! **Example (1):**
//! ```
//! use tinypool::ThreadPool;
//! use std::sync::{Arc, Mutex};
//!
//! let mut threadpool = ThreadPool::new(Some(4)).unwrap();
//! let counter = Arc::new(Mutex::new(0));
//!
//! for _ in 0..100 {
//! let counter_thrd = Arc::clone(&counter);
//! threadpool.add_to_queue(move || {
//! let mut counter = counter_thrd.lock().unwrap();
//! *counter += 1;
//! });
//! }
//!
//! threadpool.join();
//! assert_eq!(*counter.lock().unwrap(), 100);
//! ```
//!
//! **Example (2):**
//! ```
//! use tinypool::ThreadPool;
//! use std::sync::mpsc;
//!
//! let mut threadpool = ThreadPool::new(Some(4)).unwrap();
//! let (tx, rx) = mpsc::channel();
//!
//! for i in 0..100 {
//! let tx = tx.clone();
//! threadpool.add_to_queue(move || tx.send(i).unwrap());
//! }
//! drop(tx);
//!
//! let mut sum = 0;
//! while let Ok(i) = rx.recv() {
//! sum += i;
//! }
//!
//! assert_eq!(sum, 4950);
//! ```
//!
//! **Note:** The ```ThreadPool::join()``` method is automatically called when the thread pool is dropped.
pub use *;