6.1 std::thread

use std::thread::sleep_ms;
use std::sync::mpsc;
use std::thread;

// #[rustr_export]
pub fn threadinr(){
    let (tx, rx) = mpsc::sync_channel(11);

    for i in 0..10 {
        r_printf(&format!("send : {}\n",i));
        let tx = tx.clone();
        thread::spawn(move || {
            sleep_ms(500);
            tx.send(format!("from thread {}\n", i)).unwrap(); 
        });
    }

    for i in 0..10 {
        r_printf(&format!(" rec : {}  ",i));
        r_printf(&format!("{}",
                    rx.recv().unwrap_or("?".into())
                )
        );
    }
}
rust(code = '
use std::thread::sleep_ms;
use std::sync::mpsc;
use std::thread;

// #[rustr_export]
pub fn threadinr(){
    let (tx, rx) = mpsc::sync_channel(11);

    for i in 0..10 {
        r_printf(&format!("send : {}\n",i));
        let tx = tx.clone();
        thread::spawn(move || {
            sleep_ms(500);
            tx.send(format!("from thread {}\n", i)).unwrap(); 
        });
    }

    for i in 0..10 {
        r_printf(&format!(" rec : {}  ",i));
        r_printf(&format!("{}",
                    rx.recv().unwrap_or("?".into())
                )
        );
    }
}
')

threadinr()
#> send : 0
#> send : 1
#> send : 2
#> send : 3
#> send : 4
#> send : 5
#> send : 6
#> send : 7
#> send : 8
#> send : 9
#>  rec : 0  from thread 1
#>  rec : 1  from thread 2
#>  rec : 2  from thread 0
#>  rec : 3  from thread 3
#>  rec : 4  from thread 4
#>  rec : 5  from thread 5
#>  rec : 6  from thread 6
#>  rec : 7  from thread 7
#>  rec : 8  from thread 9
#>  rec : 9  from thread 8