admin管理员组

文章数量:1391805

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?

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?

Share Improve this question edited Mar 12 at 17:46 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 12 at 17:26 HarryHarry 3,2581 gold badge24 silver badges46 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 7

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.

本文标签: multithreadingAre all builtin functions thread safeStack Overflow