Im trying to build an error code which can lead to undefined mutation of variables in rust

  Kiến thức lập trình

i was trying to write an error code which can lead to undefined mutation of integer

but the same code was executed perfectly on november2023

use std::{thread, time::Duration};
use core::ptr::{write_volatile, read_volatile};

#[repr(transparent)]
struct T{
    p:*mut i32
}
struct D<Q>(Q);

// unsafe impl Sync for T {}

fn main() {
    println!("Hello, world!");
    let mut x = 0;
    let x1 = D(T{p:&mut x as *mut i32});
    let x2 = D(T{p:&mut x as *mut i32});

    let handle1 = thread::spawn(move || {
        let x1 = x1.0.p;
        unsafe {
            while read_volatile(x1) <= 10000 {
                write_volatile(x1, read_volatile(x1) + 1);
            }
        }
    });

    let handle2 = thread::spawn(move || {
        let x2 = x2.0.p;
        unsafe {
            while read_volatile(x2) <= 10000 {
                write_volatile(x2, read_volatile(x2) + 1);
            }
        }
    });

    handle1.join().unwrap();
    handle2.join().unwrap();

    // thread::sleep(Duration::from_secs(12));
    println!("{}", unsafe { read_volatile(&x as *const i32) });
}


 

    unsafe impl<Q> Send for D<Q> {}
        unsafe impl<Q> Send for D<Q> {}i well knew that the struct 'D' was unnessocery     `enter code here`according to my knowledge 

i coudnt figure out why it raise unbound errors
could someone plz figure it out!

***the error is 
error[E0277]: `*mut i32` cannot be sent between threads safely
   --> src/main.rs:16:33
    |
16  |       let handle1 = thread::spawn(move || {
    |                     ------------- ^------
    |                     |             |
    |  ___________________|_____________within this `{closure@src/main.rs:16:33: 16:40}`
    | |                   |
    | |                   required by a bound introduced by this call
17  | |         let x1 = x1.0;
18  | |         unsafe {
19  | |             while read_volatile(x1) <= 10000 {
...   |
22  | |         }
23  | |     });
    | |_____^ `*mut i32` cannot be sent between threads safely
    |
    = help: within `{closure@src/main.rs:16:33: 16:40}`, the trait `Send` is not implemented for `*mut i32`
note: required because it's used within this closure
   --> src/main.rs:16:33
    |
16  |     let handle1 = thread::spawn(move || {
    |                                 ^^^^^^^
note: required by a bound in `spawn`
   --> /home/loki/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:681:8
    |
678 | pub fn spawn<F, T>(f: F) -> JoinHandle<T>
    |        ----- required by a bound in this function
...
681 |     F: Send + 'static,
    |        ^^^^ required by this bound in `spawn`
For more information about this error, try `rustc --explain E0277`.
warning: `rust-CustomVecDequeue` (bin "rust-CustomVecDequeue") generated 1 warning
error: could not compile `rust-CustomVecDequeue` (bin "rust-CustomVecDequeue") due to 1    previous error; 1 warning emitted***

the error states that the *mut i32 doesnt include Send and Sync trait
but i explecitly defined a struct and implemented Send and Sync trait

the same was executed on november 2023 without any error
could someone rectify where is the problem or what im doing wrong

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT