#print #queue #thread #io #add #different

print_queues

A print queues that can be add from different thread and print on main thread

7 stable releases

1.0.9 Nov 29, 2023
1.0.8 Oct 5, 2022
1.0.7 Sep 5, 2022
1.0.3 Sep 1, 2022

#199 in Concurrency

29 downloads per month

MIT/Apache

5KB
68 lines

Print Queues

A print queue that can be add from different thread and print on main thread

just use tracing fr fr this is project to learn how to publish on crates.io

Usage

Simple Usage

struct Person<'a> {
    name: &'a str,
}

impl<'a> std::fmt::Display for Person<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Hello: {}", self.name)
    }
}

fn main() {
    let person = Person { name: "John doe" };

    print_queues::init();

    print_queues::add("John Doe");

    print_queues::add(person);

    print_queues::print();
    /*
        "John Doe"
        "Hello: John doe"
    */
}
print_queues::add("John");
print_queues::add("Doe");

let r = print_queues::next().unwrap();
// John

let r = print_queues::next().unwrap();
// Doe

Thread Usage

fn main() {
    print_queues::init();

    let th = std::thread::spawn(move || {
        // some server or application loop that want to print
        print_queues::add("Hello, Server!");

        std::thread::sleep(
            std::time::Duration::from_secs(3)
        );

    });

    while !th.is_finished() {
        print_queues::print();
        std::thread::sleep(
            std::time::Duration::from_millis(1)
        );
    }
}

Dependencies

~25KB