# Acquiring a per-thread mutex in rayon kills parallelism

**URL:** <https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931>\
**Category:** help\
**Created:** [January 27, 2024, 2:19pm UTC](https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931 "2024-01-27T14:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoidberg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zoidberg/32/38555_2.png) [@zoidberg](https://users.rust-lang.org/u/zoidberg)\
**Post date:** [January 27, 2024, 2:19pm UTC](https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931/1 "2024-01-27T14:19:13Z")

</div>

Hello,

The following snippet ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=987f975116a2cc821d8cb0cb54ed8855)) creates a rayon threadpool, a vector with per-thread data behind mutexes, and processes data by chunks.

```rust
use rayon::prelude::*;
use std::sync::{Arc, Mutex};
fn main() -> anyhow::Result<()> {
    // Create pool
    let pool_size = 10;
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(pool_size)
        .build()
        .unwrap();

    // Per-thread data behind a mutex
    let per_thread: Vec<Arc<Mutex<()>>> = vec![Default::default(); pool_size];

    let data = vec![10.0; 100];
    pool.install(|| {
        data.into_par_iter()
            .chunks(100 / pool_size)
            .map(|_x| {
                let thread = rayon::current_thread_index().unwrap();
                println!("start in {}", thread);
                let data = per_thread[thread].lock();
                std::thread::sleep(std::time::Duration::from_secs(1));
                println!("done in {}", thread);
            })
            .collect::<Vec<_>>();
    });

    Ok(())
}

```

The 10 tasks start simultaneously in different threads as expected:

```rust
start in 0
start in 7
start in 1
start in 2
start in 9
start in 5
start in 3
start in 8
start in 4
start in 6

```

However, they execute sequentially!

```rust
done in 1
[1 second]
done in 5
[1 second]
...

```

Removing the `.lock()` call restores parallel execution. Replacing `lock` by `try_lock` panics. This should not be the case as each thread should acquire a different mutex...

The same happens with the simpler example

```rust
vec![vec![1, 2, 3], vec![4, 5, 6]].into_par_iter().map(|_x| { ... })

```

On the other hand, doing `pool.spawn` manually executes the tasks in parallel.

I feel like I am missing something obvious in either Rayon's model, or in the behaviour of mutexes... I know that rayon performs work stealing, but I assume a function should not move to another thread once it has started executing.

Does anyone see an explanation?

---

<div class="post-metadata">

**Author:** ![jw013](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jw013/32/30963_2.png) [@jw013](https://users.rust-lang.org/u/jw013)\
**Post date:** [January 27, 2024, 4:14pm UTC](https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931/2 "2024-01-27T16:14:24Z")

</div>

> [@zoidberg](#):
>
> ```rust
> let per_thread: Vec<Arc<Mutex<()>>> = vec![Default::default(); pool_size];
> 
> ```

This initializes `per_thread` with a bunch of `Arc` clones sharing the same mutex. Initialize it with something like `(0..pool_size).map(|_| Default::default()).collect();` instead if you want a new mutex for each thread.

---

<div class="post-metadata">

**Author:** ![zoidberg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zoidberg/32/38555_2.png) [@zoidberg](https://users.rust-lang.org/u/zoidberg)\
**Post date:** [January 27, 2024, 4:18pm UTC](https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931/3 "2024-01-27T16:18:50Z")

</div>

Thanks, I missed the obvious... That trap is even described in the documentation:

> **[vec in std - Rust](https://doc.rust-lang.org/std/macro.vec.html)**
>
> Creates a \`Vec\` containing the arguments.

> This will use `clone` to duplicate an expression, so one should be careful using this with types having a nonstandard `Clone` implementation. For example, `vec![Rc::new(1); 5]` will create a vector of five references to the same boxed integer value, not five references pointing to independently boxed integers.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 26, 2024, 4:19pm UTC](https://users.rust-lang.org/t/acquiring-a-per-thread-mutex-in-rayon-kills-parallelism/105931/4 "2024-04-26T16:19:35Z")

</div>

Closed on April 26, 2024, 4:19pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
