#thread #parallel

unknownrori-simple-thread-pool

一个轻量级的线程池,用于网络和其他功能

3 个不稳定版本

0.2.0 2023年5月13日
0.1.5 2023年5月13日
0.1.4 2023年4月27日

552并发 中排名

Download history 5/week @ 2024-03-10 19/week @ 2024-03-31 50/week @ 2024-04-14

每月下载 65

MIT 许可证

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