-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does rayon::spawn
block until a thread is available?
#522
Comments
Yes and no. The caller of Also, while I expect your "million threads" was hyperbole, if you really have a lot of things to spawn then structuring it as a |
Okay, so this is a really bad idea:
Instead you should do something like:
I think there needs to be some discussion at the doc level of what rayon is good for and what it isn't good for. It seems to me (and correct me if I'm wrong) that pretty much its only purpose is to process data structures in parallel. This is a very worthwhile goal! I'm doing a review of whether to include rayon automatically in the |
Just to be clear, I mistakingly thought that rayon could be a good "thread pool" because of |
The ideal for rayon is to turn that So if your "really bad idea" was actually something like this: for n in my_vec.iter().filter(...).map(...) {
rayon::spawn(|| { /* some work with n */ })
} Then you would do better to write it more like: my_vec.par_iter().filter(...).map(...)
.for_each(|n| { /* some work with n */ }); And since your latter example shows collecting results, note that we do have
Rayon works best with tree-like dependencies, which is what While There's some work in #489 to use "fibers", which may alleviate some of this problem, at least where job dependencies could get inverted. We haven't decided how to proceed with that yet. |
Cool, thanks for the clarifications here and in the As a casual observer I'm wondering what the difficulty is in implementing a naive auto-threadpool for generic iterator. It is kind of annoying that I have to create my own threadpool to do "real" work when I could be using the rayon one. The main use case for
There is currently no good way to do this with rayon as far as I can tell. It doesn't have to be "rediculously fast" I would just like to be able to leverage the fact that I already have a threadpool for me to use! |
Hmm... looking at it more maybe this is a solution: (adapting the original example)
There would be a couple of qualifications:
|
It's not that simple. This will create tasks, but that is a pretty cheap price (it allocates a closure in the heap and pushes it onto a deque) and you have to pay it eventually anyway. That said, the loop with scope-spawn is probably not the best choice; everything else that @cuviper said applies (and, if you use a parallel iterator, you get the added benefit that we will try to lump together multiple items into one task). |
I think this is somewhat related to what I'm trying to work around in #544. Specifically, I want to use For example, methods like a) seeing how many outstanding requests there are; b) blocking until a spawned job has been taken by a worker; and c) dropping the pool without finishing all spawned jobs, would be necessary to deal with use-cases like that. Maybe even a EDIT: In fact, maybe it would be a good idea to spin off just the thread pool into its own crate, where such features could be added and maintained? And then |
I too would love to be able to access the "rayon thread pool" as it's own independent entity with a more full-featured API for a thread pool (but more light-weight/transparent than rayon itself). |
My plan for that use case was to support spawning futures, but we haven't made much progress on that. I am trying to put some time into rayon on a regular basis now, though, so I hope to make progress there. I have to sort of bring it all back into my brain to remember what my plan was tho =) @jonhoo / @vitiral — if either of you are interested in helping, let me know |
I believe that |
If you worry about atomics, then almost no cross-thread communication is possible. But that's not usually considered "blocking", even in rudimentary cases where they are implemented as compare-exchange loops. |
This is not currently documented. IMO it should block until a thread is available, to prevent firing off a million threads.
Thanks!
The text was updated successfully, but these errors were encountered: