Hacker News new | past | comments | ask | show | jobs | submit login

Ok, I guess it would be nice then if code which is unsafe but still thread-safe can be marked as such, and Rust should give a warning if used incorrectly. That way, you can parallelize any piece of code without having to think about it.



Almost all code ends up going through unsafe, as calling into the OS to do any I/O ends up being unsafe. A warning like this would warn on almost all useful programs, making the warning useless.

You already don't need to think about it. If you're not calling unsafe yourself, then it's not your job.


What I meant is that sometimes, when you have to write unsafe code, it's easier to make it not thread-safe. Then later on, someone uses that piece of code and tries to parallelize it, and runs into problems. This can be prevented with language support.


Rust does disallow you from doing some kinds of unsafe operations between threads, unless you explicitly opt in.

That said, unsafe is not generally more convenient than safe and so people empirically don’t do this in the first place.


Let me ask you then: would you use a Rust library in multithreaded code, without checking if that library is thread-safe? Wouldn't you like your compiler to tell you?


All the time.

The compiler does tell you if it’s thread safe. That’s the point.


There is so called marker traits Send and Sync that are used to track whether something is tread-safe or not. https://doc.rust-lang.org/beta/nomicon/send-and-sync.html Regardless of whether you use unsafe or not, thread safety is tracked with these and they allow the compiler to detect and prohibit data races.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: