Lockfree map example

I was experimenting with lockfree data structures inspired by this talk. I found this crate tux_lockfree which is a fork of lockfree crate (now unmaintained).

If you look at the multithreaded map example here, it is concurrently manipulating the key, under miri it runs fine.

But I am trying to update a single key from multiple threads, which makes miri fail.

        let map = Arc::new(Map::new());
        let mut threads = Vec::new();
        for _ in 1i64..=10 {
            let map = map.clone();

            threads.push(thread::spawn(move || {
                let prev = map.get("counter").map_or(1, |guard| *guard.val());
                map.insert("counter", prev + 1);

                // this one also fails
                // map.insert_with("counter", |_, _, stored| {
                    // Preview::New(stored.map_or(1, |&(_, x)| x + 1))
                // });
            }));
        }

        for thread in threads {
            thread.join().expect("thread failed");
        }

        assert_eq!(*map.get("counter").unwrap().val(), 10);

Maybe reading this topic will help you?

Maybe give DashMap a try instead of tux_lockfree?

I wanted to try lockfree data structures. I found flurry and found it easy to use. I will take a look at those links. Thanks.

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.