Code Monkey home page Code Monkey logo

Comments (2)

taiki-e avatar taiki-e commented on June 13, 2024 4

Please read into_owned's docs:

https://docs.rs/crossbeam-epoch/latest/crossbeam_epoch/struct.Shared.html#method.into_owned

This method may be called only if the pointer is valid and nobody else is holding a reference to the same object.

Your code is calling into_owned on a pointer to the same object from multiple threads (and modifying pointee), causing data races. Here is an output from miri:

error: Undefined Behavior: Data race detected between (1) Write on thread `<unnamed>` and (2) Write on thread `<unnamed>` at alloc1050. (2) just happened here
   --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/crossbeam-epoch-0.9.15/src/atomic.rs:208:9
    |
208 |         &mut *(ptr as *mut T)
    |         ^^^^^^^^^^^^^^^^^^^^^ Data race detected between (1) Write on thread `<unnamed>` and (2) Write on thread `<unnamed>` at alloc1050. (2) just happened here
    |
help: and (1) occurred earlier here
   --> src/main.rs:20:21
    |
20  |                     new_obj.count += 1;
    |                     ^^^^^^^^^^^^^
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
    = note: BACKTRACE (of the first span):
    = note: inside `<Obj as crossbeam_epoch::Pointable>::deref_mut::<'_>` at /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/crossbeam-epoch-0.9.15/src/atomic.rs:208:9: 208:30
    = note: inside `<crossbeam_epoch::Owned<Obj> as std::ops::DerefMut>::deref_mut` at /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/crossbeam-epoch-0.9.15/src/atomic.rs:1250:18: 1250:35
note: inside closure
   --> src/main.rs:20:21
    |
20  |                     new_obj.count += 1;
    |                     ^^^^^^^^^^^^^
    = note: inside `crossbeam_epoch::Atomic::<Obj>::fetch_update::<'_, {closure@src/main.rs:18:89: 18:98}>` at /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/crossbeam-epoch-0.9.15/src/atomic.rs:634:32: 634:42
note: inside closure
   --> src/main.rs:18:23
    |
18  |                   match obj_clone.fetch_update(Ordering::SeqCst, Ordering::SeqCst, guard, |old_obj| {
    |  _______________________^
19  | |                     let mut new_obj = unsafe { old_obj.into_owned() };
20  | |                     new_obj.count += 1;
21  | |                     Some(new_obj.into_shared(guard))
22  | |                 }) {
    | |__________________^

        let mut new_obj = unsafe { old_obj.into_owned() };
        new_obj.count += 1;
        Some(new_obj.into_shared(guard))

old_obj and shared retuened from new_obj are the same pointer here. You have changed the pointee (new_obj.count) but not the pointer itself.

Can you help me to fix this?

If you want to increment the counter concurrently, you can make the counter atomic.

In any case, if you want to change the pointee, you will likely need atomic or lock, and if you are not changing the pointer itself, you will likely not need epoch.

from crossbeam.

rain2307 avatar rain2307 commented on June 13, 2024

Thanks for your help

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.