Giter VIP home page Giter VIP logo

Comments (5)

dns2utf8 avatar dns2utf8 commented on June 19, 2024 1

Hi

Sorry this reply is so late. The reason we can not implement Sync is that the underlying channel does not implement it.
I solved the problem with a Mutex: https://play.rust-lang.org/?gist=cfd1222f7ba84f66d6338c7179a92656
If you use a local copy as in fn b then you don't loose too much speed from the mutex.
I will continue to think about that problem and hope to come up with a better solution.
If you already solved the problem, please let me know how you did it.

Cheers,
Stefan

from rust-threadpool.

 avatar commented on June 19, 2024

We should probably just put a Mutex around the Sender inside ThreadPool. Or use crossbeam-channel.

from rust-threadpool.

dns2utf8 avatar dns2utf8 commented on June 19, 2024

One requirement currently is that this crate compiles with rust 1.9.

from rust-threadpool.

threema-danilo avatar threema-danilo commented on June 19, 2024

Just as a small bump, I'm also in a situation where a Sync threadpool would be useful. The execute method takes &self, so purely from an ownership point of view sharing it with an Arc (e.g. across multiple web request handlers) would be sufficient. However, this doesn't work because internally the ThreadPool isn't Sync.

from rust-threadpool.

threema-danilo avatar threema-danilo commented on June 19, 2024

As a workaround, if it's fine to lock/unlock the mutex for every .execute() call, a wrapper like this can be used:

use std::sync::{Arc, Mutex};

use threadpool::ThreadPool;

/// A Send + Sync thread pool.
#[derive(Debug, Clone)]
pub struct SyncThreadPool {
    pool: Arc<Mutex<ThreadPool>>,
}

impl SyncThreadPool {
    /// Create a new thread pool with the specified size.
    pub fn new(num_threads: usize) -> Self {
        Self {
            pool: Arc::new(Mutex::new(ThreadPool::new(num_threads))),
        }
    }

    /// Execute a job on the thread pool.
    pub fn execute(&self, job: impl FnOnce() + Send + 'static) {
        self.pool
            .lock()
            .expect("could not lock thread pool mutex")
            .execute(job)
    }
}

This should have the same performance characteristics as if a mutex were used internally.

from rust-threadpool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.