Skip to content
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

Using rayon alongside mutex can cause random deadlocks #1

Closed
rhysnewell opened this issue Jun 17, 2020 · 0 comments
Closed

Using rayon alongside mutex can cause random deadlocks #1

rhysnewell opened this issue Jun 17, 2020 · 0 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@rhysnewell
Copy link
Owner

rhysnewell commented Jun 17, 2020

Bug 🐛

Rayon does not handle mutex like I thought it did. Occassionally results in the program deadlocking when running through a parallel iterator if that iterator is altering a mutex: rayon-rs/rayon#592

Fix 🛠️

Refer to rayon documentation here: https://docs.rs/rayon/1.3.1/rayon/iter/trait.ParallelIterator.html#method.for_each_with
Instead of using Mutex, use channel() from std::sync like so:

        // Create condensed links sender and receiver, avoiding use of mutex
        let (condensed_links_s, condensed_links_r) = channel();

        // extend the links for each anchor point by the union of all the indices
        links.par_iter().for_each_with(condensed_links_s, |s, (_, current_links)|{
            let mut anchors = current_links.clone();
            current_links.iter().for_each(|index| {
                match links.get(&index) {
                    Some(other_links) => {
                        anchors.par_extend(other_links.par_iter())
                    },
                    _ => {},
                }
            });
            s.send(anchors).unwrap();
        });

        // Collect receiver into hashset
        let condensed_links: HashSet<_> = condensed_links_r.iter().collect();

Issues 😢

Any HashMap types that are Arc<Mutex<_>> will be difficult to send through channel(). Not sure how to do that yet.

@rhysnewell rhysnewell added bug Something isn't working enhancement New feature or request labels Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant