Comments (5)
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.
We should probably just put a Mutex
around the Sender
inside ThreadPool
. Or use crossbeam-channel
.
from rust-threadpool.
One requirement currently is that this crate compiles with rust 1.9.
from rust-threadpool.
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.
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)
- Limit job-pool size HOT 5
- Shutdown threads gracefully HOT 4
- Why not remove trait FnBox ? HOT 1
- Possible memory leak HOT 2
- Question on panicking threads HOT 3
- v2.0 status? HOT 3
- The unit test ```test_shrink``` make no sense HOT 2
- Intermittent failure in `test_multi_join()` HOT 1
- API breaking changes proposal HOT 5
- Mention rayon's threadpool in 'similar libraries' section
- [libz blitz] Missing categories and keywords in Cargo.toml
- [libz blitz] Missing hyperlinks in documentation
- libz bliz tracking issues HOT 2
- Default number of workers: Look at the affinity mask instead of the number of CPUs? HOT 2
- Documentation? HOT 1
- Feature: split thread pool from work queue HOT 1
- Use sync_channel to limit the size of job queue? HOT 3
- Feature: provide a way to shutdown the thread pool HOT 3
- Hardcoded constant in example HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-threadpool.