3 个不稳定版本
0.2.0 | 2023年5月13日 |
---|---|
0.1.5 |
|
0.1.4 | 2023年4月27日 |
552 在 并发 中排名
每月下载 65 次
13KB
138 行
UnknownRori-Simple-Thread-Pool
一个专注于轻量级的线程池
🚀 使用方法
默认情况下,unknownrori-simple-thread-pool
使用 crossbeam-channel
而不是标准库提供的 mpsc
# If you want to use crossbeam-channel package
> cargo add unknownrori-simple-thread-pool
# If you want to use mpsc from rust standard library
> cargo add unknownrori-simple-thread-pool --no-default-features -F mpsc
use std::{
io::Write,
net::{TcpListener, TcpStream},
thread,
time::Duration,
};
use unknownrori_simple_thread_pool::{error::FailedToSendJob, ThreadPool};
fn handle_connection(mut stream: TcpStream) {
thread::sleep(Duration::from_secs(2));
let response = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi!";
stream.write_all(response.as_bytes()).unwrap();
thread::sleep(Duration::from_secs(2));
}
fn main() -> Result<(), ThreadPoolError> {
let pool = ThreadPool::new(2).unwrap();
let socket = TcpListener::bind("127.0.0.1:8000").unwrap();
println!("server started at http://127.0.0.1:8000");
for stream in socket.incoming() {
println!("Got stream!");
match stream {
Ok(stream) => pool.execute(|| handle_connection(stream))?,
Err(_) => eprintln!("Something is wrong!"),
}
}
Ok(())
}
🛠️ 开发
确保您已安装 cargo 和 git
# clone repository
> git clone https://github.com/UnknownRori/rs-simple-thread-pool
# enter cloned repository
> cd simple-rust-thread-pool
# build the library
> cargo build
🌟 贡献
请随意贡献,发送 pull request 或 issue,我会查看
依赖项
~76KB