/
LavrGov
/
thread_pool
Обзор
Документация
Войти
/
LavrGov
/
thread_pool
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
rust_pool/src/thread_pool.rs
49 строк
1 KB
Lavrentiy
Realizing your own simple thread pool in c++ and rust
08 май 2025, 22:19
08 май 2025, 22:19
0fc8cf1
Код
Авторство
О чём код?
use crate::queue::BlockingQueue; use std::{sync::atomic::AtomicBool, thread}; // Проблемы с передачей через несколько потоков type Task1 = Box<dyn FnOnce() -> () + Send + 'static>; type Task = fn() -> (); pub struct ThreadPool { all_tasks: BlockingQueue<Task>, end_pool: AtomicBool, all_workers: Vec<thread::JoinHandle<()>>, } impl ThreadPool { pub fn new(count: usize) -> Self { ThreadPool::create_workers(count) } pub fn spawn(& mut self, new_f: fn() -> ()) { self.all_tasks.put(new_f); } fn execute_funtion(tasks: BlockingQueue<Task>) { loop { let new_task = tasks.take(); new_task(); } } fn create_workers( count: usize) -> Self { let mut new_pool = Self {all_tasks: BlockingQueue::new(), all_workers: Vec::new(), end_pool: AtomicBool::new(false)}; for _ in 0..count { let new_tasks: BlockingQueue<Task> = new_pool.all_tasks.clone(); let new_thread = std::thread::spawn(move || { ThreadPool::execute_funtion(new_tasks); }); new_pool.all_workers.push(new_thread); } new_pool } pub fn join(& mut self) { self.end_pool.store(true, std::sync::atomic::Ordering::Relaxed); } }