Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 458
Harry Avatar asked Apr 17 '26 09:04

Harry


1 Answers

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.

like image 196
kmdreko Avatar answered Apr 19 '26 00:04

kmdreko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!