News for hackers. Stuff that matters.
Re:Rust: Dropping heavy things in another thread can make yo... by Ididntdothis on Saturday May 30, 2020 @01:26PM (#23362847)
I used to do this sometimes with C++ when I realized that clearing out a vector with lots of objects was slow. Is Rust basically based on unique_ptr? One problem with this approach was that you still had to wait for these threads when the application would shut down.
Re:Rust: Dropping heavy things in another thread can make yo... by masklinn on Saturday May 30, 2020 @03:21PM (#23363848)
> Is Rust basically based on unique_ptr?

Rust is based on ownership and statically checked move semantics (by default though can be opted out). So each item has a single owner (which is why Rust deals very badly with graphs, and more generally any situation where ownership is unclear) and the compiler will prevent you from using a moved object (unlike C++).

Separately it has a smart pointer which is the dual of unique_ptr (Box), with the guarantee noted above:

    let b = Box::new(1);
    drop(b);
    println!("{}", b);
will not compile because the second line moves the box, after which it can't be used because it's been removed entirely from this scope.
Re:Rust: Dropping heavy things in another thread can make yo... by wnoise on Saturday May 30, 2020 @04:02PM (#23364155)
> which is why Rust deals very badly with graphs, and more generally any situation where ownership is unclear

To be fair, so do 90+% of programmers. Much of rust's benefit in safe code is training programmers to avoid code like that where possible, and spreading design patterns that avoid it.

Re:Rust: Dropping heavy things in another thread can make yo... by saagarjha on Saturday May 30, 2020 @01:28PM (#23362868)
Rust basically gives the compiler understanding of unique_ptr and prevents you from using it after you’ve moved it.
Re:Rust: Dropping heavy things in another thread can make yo... by Ididntdothis on Saturday May 30, 2020 @01:35PM (#23362946)
Would you have to keep track of these threads in Rust? I have done a lot of desktop development where you have to be aware of what happens during shutdown. Seems a lot of server guys write their code under the assumption that it will never shut down.
Re:Rust: Dropping heavy things in another thread can make yo... by pornel on Saturday May 30, 2020 @01:59PM (#23363159)
You would need to add `thread.join()` at the end of main, or have some RAII guard that does it for you.

In practice that's probably optional, because the heap and all resources are usually torn down with the process anyway. Important things, like saving data or committing transactions, shouldn't be done in destructors.

Re:Rust: Dropping heavy things in another thread can make yo... by firethief on Saturday May 30, 2020 @01:45PM (#23363036)
The answer to this question is the same for any language without a heavy runtime. You can choose to join the worker thread, detach it, or kill it.
Re:Rust: Dropping heavy things in another thread can make yo... by zozbot234 on Saturday May 30, 2020 @02:06PM (#23363215)
> One problem with this approach was that you still had to wait for these threads when the application would shut down.

If you know that an object will live for the rest of the program and not need any finalization logic, Rust allows you to "leak" it and save that overhead on shutdown.

Re:Rust: Dropping heavy things in another thread can make yo... by ordu on Saturday May 30, 2020 @02:14PM (#23363280)
You could have just one thread and to kill it at exit. Do not start new threads for each closure that drops object, send closures into one special thread instead.
Re:Rust: Dropping heavy things in another thread can make yo... by qcoh on Saturday May 30, 2020 @01:48PM (#23363056)
Out of curiosity, how did you do that in C++?
Re:Rust: Dropping heavy things in another thread can make yo... by Ididntdothis on Saturday May 30, 2020 @01:53PM (#23363110)
It depends. Either iterate over the vector and delete the objects or just call clear(). Obviously you have to be sure that nobody else is accessing it at the same time.