(solved) No output from thread


#1

Hi.

I just got rust running in eclipse, its the most difficult language to set up I ever came across.

I’m reading the tutorial from the second-edition-book and skipped right to “Fearless Concurrency”, the first example:

use std::thread;

fn main() {
    thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {} from the spawned thread!", i);
        }
    });

    for i in 1..5 {
        println!("hi number {} from the main thread!", i);
    }
}

according to the book, output should be:

hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!

output from the website (play function):

rustc 1.18.0 (03fc9d622 2017-06-06)
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 4 from the main thread!
hi number 1 from the spawned thread!
thread '<unnamed>' panicked at 'cannot access stdout during shutdown', /checkout/src/libcore/option.rs:794

my output:

hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 4 from the main thread!

so it seems that the thread never gets executed?

I get no errors or warnings and currently have no debug in eclipse (this will probably cost me another day to setup).

Does anyone know why this happens?


#2

Ah, sorry, i should have just read the fallowing part.

Joining the handle does the trick. There is also mentioned, that the thread might not be executed at all.