Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable statics are difficult to use correctly #269

Open
Diggsey opened this issue Dec 31, 2020 · 2 comments
Open

Mutable statics are difficult to use correctly #269

Diggsey opened this issue Dec 31, 2020 · 2 comments

Comments

@Diggsey
Copy link

Diggsey commented Dec 31, 2020

The documentation gives an example of "correct" usage of a mutable static, where a hypothetical "atomic_add" function is called.

https://doc.rust-lang.org/reference/items/static-items.html#mutable-statics

However, AIUI this code is incorrect as soon as a mutable reference is created to the static variable, as the lack of synchronization means that the mutable reference may alias.

This means that mutable statics can only be used when you already have some method of synchronization in place. Unfortunately this makes mutable statics quite useless. For example: a typical use of a mutable static might be to store a libc::sem_t semaphore type.

@RustyYato
Copy link

There was a motion to deprecate static mut entirely because of how hard it is to use correctly: rust-lang/rust/issues/53639

@RalfJung
Copy link
Member

RalfJung commented Jan 2, 2021

However, AIUI this code is incorrect as soon as a mutable reference is created to the static variable, as the lack of synchronization means that the mutable reference may alias.

Hm, yeah that code should probably use &raw mut instead of &mut (Cc rust-lang/rust#73394).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants