multithreadingrust

Are all built-in functions thread safe?


In C some inbuilt functions are not thread safe and there exists a thread safe alternative for such not thread safe functions.

For example: locatime is not thread safe, the thread safe alternative is localtime_r.

So, similarly in Rust, do we have such kind of thing (thread safe version and not thread safe versions). Or, is it guaranteed that all built-in functions are thread safe?


Solution

  • If a free function is not thread-safe it will (or should) be marked unsafe with the safety criteria documented. One example from the standard library is std::env::set_var.

    I do not know of any Rust built-in functions that are not thread-safe by choice, rather as a reflection of external choices (like environment variables above). Historically non-thread-safe functions have been a source of confusion and error (even set_var if you look into its history) - thus they are avoided and guarded against in Rust.