From 2a209b82e9d668ed6824798fb5d739ae76ab9c69 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 Jan 2021 12:55:10 +0800 Subject: [PATCH] Fix threading example for sync. --- examples/threading.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/threading.rs b/examples/threading.rs index 9e115f0a..527b9214 100644 --- a/examples/threading.rs +++ b/examples/threading.rs @@ -6,6 +6,12 @@ fn main() { // Channel: Master -> Script let (tx_master, rx_script) = std::sync::mpsc::channel(); + #[cfg(feature = "sync")] + let (tx_script, rx_script) = ( + std::sync::Mutex::new(tx_script), + std::sync::Mutex::new(rx_script), + ); + // Spawn thread with Engine std::thread::spawn(move || { // Create Engine @@ -13,10 +19,19 @@ fn main() { // Register API // Notice that the API functions are blocking + + #[cfg(not(feature = "sync"))] engine .register_fn("get", move || rx_script.recv().unwrap()) .register_fn("put", move |v: INT| tx_script.send(v).unwrap()); + #[cfg(feature = "sync")] + engine + .register_fn("get", move || rx_script.lock().unwrap().recv().unwrap()) + .register_fn("put", move |v: INT| { + tx_script.lock().unwrap().send(v).unwrap() + }); + // Run script engine .consume(