Help needed with par_iter() on a query. #15194
-
Project link for context: https://github.dev/VitalyArtemiev/bevy-boids/blob/dfc4924a713bc12bb1c12469f8e01bea5ad1973b/src/boid.rs#L105 Good time of day to you. I seem to have run into a trait/borrowchecker problem that is beyond my understanding.
It throws the following error during compilation:
It compiles if you replace par_iter() with just iter(). I'm not familiar with query internals nor am I fluent it Rust's type system, but I'm reasonably sure what I'm attempting to do should be possible. I'm guessing for parallel iteration to work, it needs to clone the query over to another thread, but cannot for some reason. Can someone point me in the right direction? I have a feeling there's a one-keyword or one-line fix for this, I just don't know where to look. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Unfortunately what you're trying to do is impossible. Fundamentally, a mutable query can only be accessed in one place at a time. With |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar problem that i am trying to solve or trying to find a workaround for that. I have a data structures where i have saved the entities that i want to read in parallel . For constraint of the problem i am sure that entities are unique in this data structure. In that case i need to modify some components, so i must use a query object. Unfortunately same like you i can't share a mutable query through another parallel context. But i am sure that i am not trying to access to the same entity from multiple threads, so i decided to try to wrap the data of a component in a mutex. With that i can modify the component's data with a non mutable query. The only concern is about performance, but i am thinking that probably, if i am sure that entities are unique, there isn't a thread that is waiting for a lock, each mutex is locked and unlocked exactly one time. I don't know if can fit your problem, and i don't know what is the performance in this case. |
Beta Was this translation helpful? Give feedback.
Unfortunately what you're trying to do is impossible. Fundamentally, a mutable query can only be accessed in one place at a time. With
par_iter
however there will be multiple threads running the given closure, and they will all try to access the mutable query at the same time, which cannot be allowed.