Hazard Pointers from Haphazard crate not Send/Sync?

Hi everyone,

I'm using the Haphazard crate for hazard pointers, and for some reason, I cant sent a simple haphazard::AtomicPtr across threads. Here's a code example:

use haphazard;
use std::sync::Arc;
use std::thread;
fn main() {
    let hp: Arc<haphazard::AtomicPtr<isize>> =
            Arc::new(haphazard::AtomicPtr::from(Box::new(5)));
    thread::spawn(|| hp);
}

This gives the following errors:

error[E0277]: `*mut leaky::alloc::boxed::Box<isize>` cannot be shared between threads safely
   --> src\hazptr_practice.rs:112:23
    |
112 |         thread::spawn(|| hp);
    |         ------------- ^^^^^ `*mut leaky::alloc::boxed::Box<isize>` cannot be shared between threads safely
    |         |
    |         required by a bound introduced by this call
    |
    = help: within `haphazard::AtomicPtr<isize>`, the trait `Sync` is not implemented for `*mut leaky::alloc::boxed::Box<isize>`
    = note: required because it appears within the type `(haphazard::Global, *mut leaky::alloc::boxed::Box<isize>)`
    = note: required because it appears within the type `PhantomData<(haphazard::Global, *mut leaky::alloc::boxed::Box<isize>)>`
    = note: required because it appears within the type `haphazard::AtomicPtr<isize>`
    = note: required because of the requirements on the impl of `Send` for `Arc<haphazard::AtomicPtr<isize>>`
note: required by a bound in `spawn`
   --> C:\Users\Goomb\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\thread\mod.rs:647:8
    |
647 |     T: Send + 'static,
    |        ^^^^ required by this bound in `spawn`

error[E0277]: `*mut leaky::alloc::boxed::Box<isize>` cannot be sent between threads safely
   --> src\hazptr_practice.rs:112:23
    |
112 |         thread::spawn(|| hp);
    |         ------------- ^^^^^ `*mut leaky::alloc::boxed::Box<isize>` cannot be sent between threads safely
    |         |
    |         required by a bound introduced by this call
    |
    = help: within `haphazard::AtomicPtr<isize>`, the trait `Send` is not implemented for `*mut leaky::alloc::boxed::Box<isize>`
    = note: required because it appears within the type `(haphazard::Global, *mut leaky::alloc::boxed::Box<isize>)`
    = note: required because it appears within the type `PhantomData<(haphazard::Global, *mut leaky::alloc::boxed::Box<isize>)>`
    = note: required because it appears within the type `haphazard::AtomicPtr<isize>`
    = note: required because of the requirements on the impl of `Send` for `Arc<haphazard::AtomicPtr<isize>>`
note: required by a bound in `spawn`
   --> C:\Users\Goomb\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\thread\mod.rs:647:8
    |
647 |     T: Send + 'static,
    |        ^^^^ required by this bound in `spawn`

For more information about this error, try `rustc --explain E0277`.

I get that a raw pointer can't be send safely across threads, but I thought that this was taken care of by the unsafe impl's for haphazard::AtomicPtr, which are here.

Thanks :slightly_smiling_face:

Sorry, accidentally posted before I was done typing! The post is edited now.

It looks like in haphazard versions 0.1.2 and earlier, AtomicPtr was !Send and !Sync. Are you sure that you're using a newer version of the crate?

1 Like

Oh shoot, I think that's it! I can't check right now, but I suspect you are correct. Thank you so much :smiley:

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.