Решение на упр.13 задача 1 от Георги Стоянов

Обратно към всички решения

Към профила на Георги Стоянов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::sync::{mpsc, Arc, Mutex};
use std::thread;
pub trait Job: Send + 'static {
type Output: Send + 'static;
type Error: Send + 'static;
fn run(self) -> Result<Self::Output, Self::Error>;
}
enum Message {
Run(Box<dyn FnOnce() + Send>),
Shutdown,
}
pub struct ThreadPool {
workers: Vec<thread::JoinHandle<()>>,
sender: mpsc::Sender<Message>,
}
impl ThreadPool {
pub fn new(worker_count: usize) -> Self {
let (sender, receiver) = mpsc::channel::<Message>();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(worker_count);
for _ in 0..worker_count {
let thread_receiver = Arc::clone(&receiver);
let handle = thread::spawn(move || {
loop {
let msg = thread_receiver
.lock()
.expect("receiver mutex poisoned")
.recv();
match msg {
Ok(Message::Run(f)) => {
f();
}
Ok(Message::Shutdown) => break,
Err(_) => break,
}
}
});
workers.push(handle);
}
Self { workers, sender }
}
pub fn submit<J>(&self, job: J) -> mpsc::Receiver<Result<J::Output, J::Error>>
where
J: Job,
{
let (result_sender, result_receiver) = mpsc::channel();
let task = Message::Run(Box::new(move || {
let res = job.run();
let _ = result_sender.send(res);
}));
let _ = self.sender.send(task);
result_receiver
}
}
impl Drop for ThreadPool {
fn drop(&mut self) {
for _ in 0..self.workers.len() {
let _ = self.sender.send(Message::Shutdown);
}
for handle in self.workers.drain(..) {
let _ = handle.join();
}
}
}

Лог от изпълнението

Updating crates.io index
     Locking 46 packages to latest compatible versions
   Compiling proc-macro2 v1.0.106
   Compiling libc v0.2.182
   Compiling unicode-ident v1.0.23
   Compiling quote v1.0.44
   Compiling syn v2.0.115
   Compiling futures-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling parking_lot_core v0.9.12
   Compiling futures-channel v0.3.31
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling cfg-if v1.0.4
   Compiling memchr v2.8.0
   Compiling scopeguard v1.2.0
   Compiling slab v0.4.12
   Compiling smallvec v1.15.1
   Compiling futures-task v0.3.31
   Compiling lock_api v0.4.14
   Compiling errno v0.3.14
   Compiling signal-hook-registry v1.4.8
   Compiling parking_lot v0.12.5
   Compiling mio v1.1.1
   Compiling futures-macro v0.3.31
   Compiling tokio-macros v2.6.0
   Compiling socket2 v0.6.2
   Compiling futures-util v0.3.31
   Compiling bytes v1.11.1
   Compiling tokio v1.49.0
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
   Compiling solution v0.1.0 (/tmp/d20260214-4108951-17k5a7b/solution)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 18.49s
     Running tests/solution_test.rs (target/debug/deps/solution_test-24c102a0f7d8d43b)

running 4 tests
test solution_test::test_scheduling ... ok
test solution_test::test_jobs_run_in_parallel ... ok
test solution_test::test_submit_queue ... ok
test solution_test::test_submit_recursive ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.20s

История (1 версия и 0 коментара)

Георги качи първо решение на 18.01.2026 12:50 (преди около 2 месеца)