thread_write/
thread_write.rs

1#![allow(unused_must_use)]
2
3extern crate stijl;
4
5use std::io::Write;
6use std::thread;
7use std::time;
8use stijl::*;
9
10// Spawns three threads and writes various styled messages that should
11// not interleave.
12fn main() {
13    let t1 = thread::spawn(|| {
14        let five_millis = time::Duration::from_millis(5);
15        let stream = stijl::stdout(DoStyle::Auto);
16        let mut stream = stream.lock();
17        stream.fg(Blue);
18        write!(stream, "ONE ");
19        thread::sleep(five_millis);
20        write!(stream, "TWO ");
21        thread::sleep(five_millis);
22        stream.em();
23        writeln!(stream, "THREE");
24        stream.reset();
25    });
26    let t2 = thread::spawn(|| {
27        let five_millis = time::Duration::from_millis(5);
28        let stream = stijl::stdout(DoStyle::Auto);
29        let mut stream = stream.lock();
30        stream.fg(Green);
31        write!(stream, "1 ");
32        thread::sleep(five_millis);
33        write!(stream, "2 ");
34        thread::sleep(five_millis);
35        stream.em();
36        writeln!(stream, "3");
37        stream.reset();
38    });
39    let t3 = thread::spawn(|| {
40        let five_millis = time::Duration::from_millis(5);
41        let stream = stijl::stdout(DoStyle::Auto);
42        let mut stream = stream.lock();
43        stream.fg(Red);
44        write!(stream, "I ");
45        thread::sleep(five_millis);
46        write!(stream, "II ");
47        thread::sleep(five_millis);
48        stream.em();
49        writeln!(stream, "III");
50        stream.reset();
51    });
52
53    t1.join().unwrap();
54    t2.join().unwrap();
55    t3.join().unwrap();
56}