Comments (2)
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.
Thanks for your help
from crossbeam.
Related Issues (20)
- Implement fmt::Display for CachePadded where T: Display HOT 1
- Thread-safe, shared-state concurrency with `WaitGroup` HOT 2
- crossbeam-utils fails to compile on esp32* targets starting Rust 1.74 HOT 3
- provide a scoped thread start taht returns result HOT 1
- Potential to modify the ordering for 'load' operations in the garbage module of branch 0.2.10 HOT 4
- Move CachePadded to separate sub-crate? HOT 5
- Fine-tune the ordering for received HOT 2
- Supporting `allocator_api` HOT 1
- `#[cfg(...)]` in the select macro
- crossbeam::skiplist: make iterator sendable to anther threads HOT 1
- select_macro.rs's fairness2 test is flaky with sanitizer
- Channel `select` panics "passed a receiver that wasn't selected" when using different clones of the same receiver HOT 2
- Use SkipMap as a lock free concurrent mutable hash map? HOT 2
- Complexity O() HOT 2
- Add `with_capacity` constructor
- Potential deadlock and resource leak when `Receiver` is dropped in `crossbeam_channel`'s bounded channel HOT 1
- crossbeam-channel has better performance with binded CPU ? HOT 3
- A question regarding Miri error running the “sanitize" example under crossbeam-epoch.
- crossbeam-deque work\steal panic on wasm32-wasip1-threads target HOT 3
- Channel timeouts based on deadlines instead of durations HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crossbeam.