A friend of mine wants to learn Rust by writing a POSIX shell, but he’s decided against it. He needs to be able to fork()
so that subshells can share state cleanly (IPC would be too slow; apparently this would not be POSIX compliant, but I’m not a domain expert here). Having some std
thing spawn threads, like something that quietly calls a parallel iterator as an implementation detail, makes fork()
unsafe. (Note: fork()
makes some of the borrowck’s assumptions unsound, but even in C fork()
is a hilariouslly unsafe thing you have to take extreme care with).
Fundamentally, I think it would be valuable for std
to make guarantees about spawning threads (e.g. any method which may spawn an OS thread is specifically singled out). This is useful in other situations where you must be single-threaded, such as BoringSSL’s lockless mode for embedded. AFAIK, std
makes no such guarantees, and "don’t call thread::spawn
" is not a complete list of things to do to prevent spawning threads.
Alternatively, a compiler switch to make thread::spawn
always panic might be another option, but that seems somewhat hamfisted.