Code Monkey home page Code Monkey logo

Comments (10)

tomtomjhj avatar tomtomjhj commented on June 1, 2024 1

Unlike std's compare_exchange, Atomic::compare_exchange returns the value that was written. Not sure why it behaves differently from std.

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024

This is an occasional problem that will recur after executing "cargo run" multiple times.

from crossbeam.

taiki-e avatar taiki-e commented on June 1, 2024

I believe the error itself is the result of use-after-free.

It is wrong to use the Relaxed ordering everywhere when handling pointers, and the orderings of load in the first spawn and the success ordering of compare_exchange in the second spawn have to be Acquire (load), AcqRel (compare_exchange). The problem may not be only that, though.

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024

It seems that this is not the problem. I changed the code to use SeqCst and still get the same error.

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024

I tried changing it to this

a.load(Ordering::Acquire, guard)
a.compare_exchange(shared, Owned::new(A(i.to_string())),
                                          Ordering::AcqRel, Ordering::AcqRel, guard)

It results in an error every time it is executed

If 'SeqCst' is used, the probability of errors will be much lower, but there will still be errors

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024
            loop {
                let shared = a.load(Ordering::Relaxed, guard);
                match a.compare_exchange(shared, Owned::new(A(i.to_string())),
                                         Ordering::Relaxed, Ordering::Relaxed, guard) {
                    Ok(rs) => {
                        unsafe {
                            guard.defer_destroy(rs);
                        }
                        break;
                    }
                    Err(_e) => {
                        // shared = e.current;
                    }
                }
            }

Writing like this still leads to errors.If possible, please tell me how to modify it, thank you

from crossbeam.

tomtomjhj avatar tomtomjhj commented on June 1, 2024

defer_destroy(shared)

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024

defer_destroy(shared)

Am I using the wrong method

from crossbeam.

taiki-e avatar taiki-e commented on June 1, 2024
struct A(String);

fn main() {
    let a = Arc::new(Atomic::new(A("0".to_string())));
    for _ in 1..100 {
        let a = a.clone();
        thread::spawn(move || {
            let guard = &crossbeam_epoch::pin();
-            let mut shared = a.load(Ordering::Relaxed, guard);
+            let mut shared = a.load(Ordering::Acquire, guard);
            unsafe {
                println!("{}",shared.as_ref().unwrap().0);
            }
        });
    }
    for i in 1..100 {
        let a = a.clone();
        thread::spawn(move || {
            let guard = &crossbeam_epoch::pin();
            let mut shared = a.load(Ordering::Relaxed, guard);
            loop {
                match a.compare_exchange(shared, Owned::new(A(i.to_string())),
-                                          Ordering::Relaxed, Ordering::Relaxed, guard) {
+                                          Ordering::AcqRel, Ordering::Relaxed, guard) {
                    Ok(rs) => {
                        unsafe {
-                            guard.defer_destroy(rs);
+                            guard.defer_destroy(shared);
                        }
                        break;
                    }
                    Err(e) => {
                        shared = e.current;
                    }
                }
            }
        });
    }
}

from crossbeam.

lbl8603 avatar lbl8603 commented on June 1, 2024
                        unsafe {
-                            guard.defer_destroy(rs);
+                            guard.defer_destroy(shared);
                        }

Thank you. This has solved the above problem

from crossbeam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.