crossbeam-rs / crossbeam

Tools for concurrent programming in Rust
Apache License 2.0
7.48k stars 471 forks source link

Use SkipMap as a lock free concurrent mutable hash map? #1098

Closed yashkothari42 closed 7 months ago

yashkothari42 commented 8 months ago

I am implementing a global store in which i am using SkipMap.

  1. In main thread, create an object of SkipMap.
  2. share it to all the threads
  3. threads will insert and remove from the threads.
  4. remove on a key would be called only after an insert. would skipmap help in this kind of design?
pub fn test_function(store: SkipMap<i32, i32>) {
    println!("in function");
    loop {}
}
    let global_store: SkipMap<i32, i32> = SkipMap::new();
    scope(|s| {
        for thread_num in 0..num_threads {
            s.spawn(|_| {
                test_function(global_store);
            });
        }
    })
    .unwrap();

The above code is giving following errors, adding move in spawn also gives error that Copy in not implemented for SkipMap. What am I missing here?

 value moved into closure here, in previous iteration of loop
58 |                 test_function(global_store);
   |                               ------------ use occurs due to use in closure
   |
   = note: move occurs because `global_store` has type `SkipMap<i32, i32>`, which does not implement the `Copy` trait

It is not clear from the example, would really appreciate the inputs :)

Tianion commented 7 months ago

You should use Arc::clone.

taiki-e commented 7 months ago

Alternatively, a scope thread is used there, so it should also be possible to pass reference to map instead of pass map by value.