Compute Fibonacci numbers concurrently

    • wasmedge-sys v0.7.0
    • wasmedge-types v0.1.1

    In this example, we will demonstrate how to use the objects and the APIs defined in to compute Fibonacci numbers concurrently.

    1. #![allow(unused)]
    2. fn main() {
    3. let vm = Arc::new(Mutex::new(vm));
    4. let handle_a = thread::spawn(move || {
    5. let vm_child_thread = vm_cloned.lock().expect("fail to lock vm");
    6. let returns = vm_child_thread
    7. .run_registered_function("extern", "fib", [WasmValue::from_i32(4)])
    8. .expect("fail to compute fib(4)");
    9. let fib4 = returns[0].to_i32();
    10. println!("fib(4) by child thread: {}", fib4);
    11. fib4
    12. });
    13. let vm_cloned = Arc::clone(&vm);
    14. let vm_child_thread = vm_cloned.lock().expect("fail to lock vm");
    15. let returns = vm_child_thread
    16. .run_registered_function("extern", "fib", [WasmValue::from_i32(5)])
    17. .expect("fail to compute fib(5)");
    18. let fib5 = returns[0].to_i32();
    19. println!("fib(5) by child thread: {}", fib5);
    20. fib5
    21. });

    let fib4 = handle_a.join().unwrap(); let fib5 = handle_b.join().unwrap(); // compute fib(6) println!("fib(6) = fib(5) + fib(1) = {}", fib5 + fib4);

    fib(4) by child thread: 5 fib(5) by child thread: 8 fib(6) = fib(5) + fib(1) = 13