Mutex for extern `static mut`

⚓ rust    📅 2025-07-16    👤 surdeus    👁️ 1      

surdeus

I have a mutable stativ variable that comes from a C library.

#[link(name = "imaevm")]
extern "C" {
    static mut imaevm_params: libimaevm_params;
}

libimaevm fills this struct with values taken from options and arguments of the CLI application. This is safe since the CLI is single-threaded. But how do I ensure that calling something like this from multiple threads is safe?

fn do_stuff(some_field: *const i8) {
    unsafe {
        imaevm_params = libimaevm_params {
            some_field,
            ..imaevm_params
        };
    }
    use_imaevm_params();
}

3 posts - 2 participants

Read full topic

🏷️ rust_feed