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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! A module containing the thread pool implementation.
// IMPORTS
use std::cmp::Ordering;
use std::io;
use std::sync::{Arc, mpsc, Mutex};
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
use std::thread;
// ENUMS
/// The messages that can be sent to the worker threads.
enum Message {
/// A job to execute in the worker thread.
Job(Box<dyn FnOnce() + Send + 'static>),
/// Terminate the worker thread.
Terminate,
/// The worker thread with the given ID is closing.
Closing(usize),
/// The queue is empty. There are no more jobs to execute.
EmptyQueue,
/// Continue executing jobs. (unpause)
Continue,
}
// STRUCTS
/// A thread pool that manages threads and executes jobs in them.
pub struct ThreadPool {
/// The workers in the thread pool.
workers: Vec<Worker>,
/// The transmitting end of the channel to communicate info messages from the worker threads.
worker_info_up_tx: mpsc::Sender<Message>,
/// The receiving end of the channel to communicate info messages from the worker threads.
worker_info_up_rx: mpsc::Receiver<Message>,
/// The transmitting end of the channel to communicate info messages to the worker threads.
worker_info_down_tx: mpsc::Sender<Message>,
/// The receiving end of the channel to communicate info messages to the worker threads.
worker_info_down_rx: Arc<Mutex<mpsc::Receiver<Message>>>,
/// The transmitting end of the channel to communicate jobs to the worker threads.
queue_tx: mpsc::Sender<Message>,
/// The receiving end of the channel to communicate jobs to the worker threads.
queue_rx: Arc<Mutex<mpsc::Receiver<Message>>>,
/// The number of jobs in the queue.
queued_jobs: Arc<AtomicUsize>,
}
impl ThreadPool {
/// Create a new thread pool with the given size.
/// # Arguments
/// * `size` - The number of threads to create in the thread pool. Use `None` to determine the number of threads automatically.
/// # Returns
/// A ```Result``` containing the ```ThreadPool``` if the creation was successful, or an ```io::Error``` if it was unsuccessful.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// // Create a thread pool with 4 threads
/// let threadpool = ThreadPool::new(Some(4)).unwrap();
/// // Create a thread pool with the number of threads determined automatically
/// let threadpool2 = ThreadPool::new(None).unwrap();
/// ```
pub fn new(size: Option<usize>) -> Result<Self, io::Error> {
let size = size.unwrap_or(thread::available_parallelism()?.get());
let workers = Vec::with_capacity(size);
let (worker_info_up_tx, worker_info_up_rx) = mpsc::channel::<Message>();
let (worker_info_down_tx, worker_info_down_rx) = mpsc::channel::<Message>();
let worker_info_down_rx = Arc::new(Mutex::new(worker_info_down_rx));
let (queue_tx, queue_rx) = mpsc::channel::<Message>();
let queue_rx = Arc::new(Mutex::new(queue_rx));
let queued_jobs = Arc::new(AtomicUsize::new(0));
let mut threadpool = Self {
workers,
worker_info_up_tx,
worker_info_up_rx,
worker_info_down_tx,
worker_info_down_rx,
queue_tx,
queue_rx,
queued_jobs,
};
threadpool.set_size(Some(size))?;
Ok(threadpool)
}
/// Add a job to the queue. Jobs are executed in the order they are added.
/// If there are no available threads, the job will be queued until a thread is available.
/// # Arguments
/// * `job` - The job (closure) to execute in a thread pool.
/// # Examples
/// ```
/// 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);
/// ```
pub fn add_to_queue<F>(&self, job: F)
where
F: FnOnce() + Send + 'static,
{
self.queued_jobs.fetch_add(1, AtomicOrdering::Release);
self.queue_tx.send(Message::Job(Box::new(job))).unwrap();
}
/// Get the number of queued (and running) jobs.
/// # Returns
/// * ```usize``` - The number of queued jobs.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// let mut threadpool = ThreadPool::new(Some(0)).unwrap();
///
/// for i in 0..8 {
/// threadpool.add_to_queue(move || { println!("{i}"); });
/// }
/// assert_eq!(threadpool.queued_jobs(), 8); // 8 jobs queued
///
/// threadpool.set_size(Some(4)).unwrap();
/// threadpool.join();
/// assert_eq!(threadpool.queued_jobs(), 0); // 0 jobs
/// ```
pub fn queued_jobs(&self) -> usize {
self.queued_jobs.load(AtomicOrdering::Acquire)
}
/// Get the number of threads in the thread pool.
/// # Returns
/// * ```usize``` - The number of threads in the thread pool.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// let threadpool = ThreadPool::new(Some(4)).unwrap();
/// assert_eq!(threadpool.size(), 4);
///
/// let threadpool2 = ThreadPool::new(Some(0)).unwrap();
/// assert_eq!(threadpool2.size(), 0);
/// ```
pub fn size(&self) -> usize {
self.workers.len()
}
/// Set the number of threads in the thread pool.
/// If increasing the number of threads, this method will not block the main thread.
/// If reducing the number of threads, this method will put closing messages in the queue for the worker threads to receive.
/// So, this method will block the main thread until all those closing messages are received.
/// That means that all jobs before the closing messages need to be processed.
/// Note that some jobs may still be executing in the remaining worker threads, so the ```queued_jobs()``` method may return a non-zero value.
/// # Arguments
/// * `size` - The number of threads to set the thread pool size to. Use `None` to determine the number of threads automatically.
/// # Returns
/// A ```Result``` containing ```()``` if successful, or an ```io::Error``` if unsuccessful.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// let mut threadpool = ThreadPool::new(Some(0)).unwrap();
/// assert_eq!(threadpool.size(), 0);
///
/// for i in 0..8 {
/// threadpool.add_to_queue(move || { println!("{i}"); });
/// }
/// assert_eq!(threadpool.queued_jobs(), 8);
///
/// // increasing thread pool size doesn't block the main thread
/// threadpool.set_size(Some(8)).unwrap();
/// assert_eq!(threadpool.size(), 8);
/// // jobs are now being executed
///
/// // decreasing thread pool size blocks the main thread while waiting for all queued jobs to finish
/// threadpool.set_size(Some(2)).unwrap();
/// assert_eq!(threadpool.size(), 2);
/// // threadpool.queued_jobs() may return a non-zero value here (the value is guaranteed to be <= threadpool.size())
/// assert!(threadpool.queued_jobs() <= threadpool.size());
/// ```
pub fn set_size(&mut self, size: Option<usize>) -> Result<(), io::Error> {
let new_size = size.unwrap_or(thread::available_parallelism()?.get());
let current_size = self.size();
match new_size.cmp(¤t_size) {
Ordering::Less => {
let reduce = current_size - new_size;
for _ in 0..reduce {
self.queue_tx.send(Message::Terminate).unwrap();
}
for _ in 0..reduce {
match self.worker_info_up_rx.recv().unwrap() {
Message::Closing(id) => {
self.workers.retain(|worker| worker.id != id);
},
_ => panic!("Received unexpected message from worker thread."),
}
}
self.workers.shrink_to_fit();
},
Ordering::Equal => {},
Ordering::Greater => {
let additional_size = new_size - current_size;
self.workers.reserve_exact(additional_size);
let mut id = 0;
for _ in 0..additional_size {
while self.workers.iter().any(|worker| worker.id == id) {
id += 1;
}
self.workers.push(
Worker::new(
id,
self.worker_info_up_tx.clone(),
Arc::clone(&self.worker_info_down_rx),
Arc::clone(&self.queue_rx),
Arc::clone(&self.queued_jobs),
)?
);
}
},
}
Ok(())
}
/// Wait for all queued jobs to finish and close all threads.
/// This method will block until all queued jobs have finished.
/// It will then close all threads in the thread pool.
/// If you want to wait for all queued jobs to finish, but want to keep the threads running, use ```ThreadPool::wait()``` instead.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// let mut threadpool = ThreadPool::new(Some(4)).unwrap();
/// assert_eq!(threadpool.size(), 4);
/// assert_eq!(threadpool.queued_jobs(), 0);
///
/// for i in 0..8 {
/// threadpool.add_to_queue(move || { println!("{i}"); });
/// }
///
/// threadpool.join();
/// assert_eq!(threadpool.size(), 0);
/// assert_eq!(threadpool.queued_jobs(), 0);
/// ```
pub fn join(&mut self) {
let num_of_workers = self.workers.len();
for _ in 0..num_of_workers {
self.queue_tx.send(Message::Terminate).unwrap();
}
for worker in self.workers.drain(..) {
worker.thread.join().unwrap();
}
self.workers.shrink_to_fit();
for _ in 0..num_of_workers {
match self.worker_info_up_rx.recv().unwrap() {
Message::Closing(_) => {},
_ => panic!("Received unexpected message from worker thread."),
}
}
}
/// Wait for all queued jobs to finish.
/// This method will block until all queued jobs have finished.
/// If you want to wait for all queued jobs to finish and close all threads, use ```ThreadPool::join()``` instead.
/// # Examples
/// ```
/// use tinypool::ThreadPool;
///
/// let mut threadpool = ThreadPool::new(Some(4)).unwrap();
/// assert_eq!(threadpool.size(), 4);
/// assert_eq!(threadpool.queued_jobs(), 0);
///
/// for i in 0..8 {
/// threadpool.add_to_queue(move || { println!("{i}") });
/// }
///
/// threadpool.wait();
/// assert_eq!(threadpool.size(), 4);
/// assert_eq!(threadpool.queued_jobs(), 0);
/// ```
pub fn wait(&self) {
// send empty queue messages to all workers
let pool_size = self.size();
for _ in 0..pool_size {
self.queue_tx.send(Message::EmptyQueue).unwrap();
}
// every worker, when it receives an empty queue message, will echo that message in worker_info_up channel
// and pause and wait for continue message in the info channel
// we need to receive the empty queue messages from all workers in the worker_info_up channel
// when we do, that means all workers are paused and waiting for continue message
// that is all jobs have finished executing
for _ in 0..pool_size {
match self.worker_info_up_rx.recv().unwrap() {
Message::EmptyQueue => {},
_ => panic!("Received unexpected message from worker thread."),
}
}
// send continue messages to all workers
for _ in 0..pool_size {
self.worker_info_down_tx.send(Message::Continue).unwrap();
}
}
}
impl Drop for ThreadPool {
/// Drop the thread pool.
/// This will wait for all queued jobs to finish and close all threads.
/// Equivalent to ```ThreadPool::join()```.
fn drop(&mut self) {
self.join();
}
}
/// A worker struct, used to spawn a worker thread.
struct Worker {
/// The id of the worker.
id: usize,
/// The thread of the worker.
thread: thread::JoinHandle<()>,
}
impl Worker {
/// Create a new worker struct.
/// # Arguments
/// * `id` - The id of the worker.
/// * `worker_info_up_tx` - The transmitting end of the channel to communicate info messages from the worker threads.
/// * `worker_info_down_rx` - The receiving end of the channel to communicate info messages to the worker threads.
/// * `queue_rx` - The receiving end of the channel to communicate jobs to the worker threads.
/// * `queued_jobs` - The number of jobs in the queue.
/// # Returns
/// A ```Result``` containing the ```Worker``` if successful, or an ```io::Error``` if unsuccessful.
fn new(id: usize, worker_info_up_tx: mpsc::Sender<Message>, worker_info_down_rx: Arc<Mutex<mpsc::Receiver<Message>>>, queue_rx: Arc<Mutex<mpsc::Receiver<Message>>>, queued_jobs: Arc<AtomicUsize>) -> Result<Self, io::Error> {
let thread = thread::Builder::new()
.spawn(move || {
loop {
let msg = queue_rx.lock().unwrap().recv().unwrap();
match msg {
Message::Job(job) => {
job();
queued_jobs.fetch_sub(1, AtomicOrdering::Release);
},
Message::Terminate => {
worker_info_up_tx.send(Message::Closing(id)).unwrap();
break;
},
Message::EmptyQueue => {
worker_info_up_tx.send(Message::EmptyQueue).unwrap();
match worker_info_down_rx.lock().unwrap().recv().unwrap() {
Message::Continue => {},
_ => panic!("Received unexpected message from thread pool."),
}
},
_ => panic!("Received unexpected message from thread pool."),
}
}
})?;
Ok(Self { id, thread })
}
}
// TESTS
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new() {
let pool = ThreadPool::new(Some(4)).unwrap();
assert_eq!(pool.size(), 4);
assert_eq!(pool.queued_jobs(), 0);
}
#[test]
fn size() {
let mut pool = ThreadPool::new(Some(4)).unwrap();
assert_eq!(pool.size(), 4);
pool.set_size(Some(8)).unwrap();
assert_eq!(pool.size(), 8);
pool.set_size(Some(2)).unwrap();
assert_eq!(pool.size(), 2);
pool.set_size(Some(0)).unwrap();
assert_eq!(pool.size(), 0);
pool.set_size(None).unwrap();
assert_eq!(pool.size(), thread::available_parallelism().unwrap().get());
}
#[test]
fn queue() {
let pool = ThreadPool::new(Some(0)).unwrap();
assert_eq!(pool.queued_jobs(), 0);
for i in 0..8 {
pool.add_to_queue(move || { println!("{i}"); });
}
assert_eq!(pool.queued_jobs(), 8);
}
#[test]
fn join() {
let mut pool = ThreadPool::new(Some(4)).unwrap();
assert_eq!(pool.size(), 4);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.join();
assert_eq!(pool.queued_jobs(), 0); // all jobs have finished
assert_eq!(pool.size(), 0); // all threads have been closed
}
#[test]
fn wait() {
let pool = ThreadPool::new(Some(4)).unwrap();
assert_eq!(pool.size(), 4);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.wait();
std::thread::sleep(std::time::Duration::from_millis(100));
assert_eq!(pool.queued_jobs(), 0); // all jobs have finished
assert_eq!(pool.size(), 4); // all threads are still running
}
#[test]
fn complex() {
let mut pool = ThreadPool::new(Some(4)).unwrap();
assert_eq!(pool.size(), 4);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.set_size(Some(8)).unwrap();
assert_eq!(pool.size(), 8);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.wait();
assert!(pool.queued_jobs() <= pool.size());
pool.set_size(Some(2)).unwrap();
assert_eq!(pool.size(), 2);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.set_size(Some(0)).unwrap();
assert_eq!(pool.size(), 0);
for i in 0..1000 {
pool.add_to_queue(move || { println!("{i}"); });
}
pool.set_size(None).unwrap();
assert_eq!(pool.size(), thread::available_parallelism().unwrap().get());
pool.join();
assert_eq!(pool.queued_jobs(), 0); // all jobs have finished
assert_eq!(pool.size(), 0); // all threads have been closed
}
}